question on template, traits class and static inline

H

Hong Ye

Traits is a useful template technique to simplfy the implementation of some
classes. however, I met some questions when I tried to understand and
implement it.
Following is an example of traits template with specializations (copied
from Nathan C. Myer's 1995 article):

template <class numT>
struct float_traits { };

struct float_traits<float> {
typedef float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
...
};

struct float_traits<double> {
typedef double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
...
};
Here are my questions,1) Are "float_traits" really a C++ class? I saw many
people call this as traits class. but I cannot find keyword "class" in the
definition.2) Is "static inline" keyword a requirement for member functions
within traits? what is the benefit of using static keyword here?any kind of
reply is appreciated.hong
 
V

Victor Bazarov

Hong said:
Traits is a useful template technique to simplfy the implementation
of some classes. however, I met some questions when I tried to
understand and implement it.
Following is an example of traits template with specializations
(copied from Nathan C. Myer's 1995 article):

template <class numT>
struct float_traits { };

struct float_traits<float> {
typedef float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
...
};

struct float_traits<double> {
typedef double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
...
};
Here are my questions,1) Are "float_traits" really a C++ class?

No, 'float_traits' is a class template.
I saw
many people call this as traits class. but I cannot find keyword
"class" in the definition.

Can you find "struct"? It's the same thing.
2) Is "static inline" keyword a requirement
for member functions within traits?

No, but since template implementation is very likely to reside in
a header, they better be "inline" (or the ODR is violated). The
'inline' for a function defined inside a class is superfluous.
Any function defined inside the class definition is inline.
what is the benefit of using
static keyword here?any kind of reply is appreciated.hong

'static' doesn't require to have an instance of the class to call
the function (since it doesn't need an instance anyway).

V
 
H

Hong Ye

Hi Vic,

thanks a lot for the explanation. by the way, what does "ODR" mean?
hong
 
R

red floyd

Hong Ye wrote:
[redacted]


[top posting corrected. All sigs removed]

> Hi Vic,
>
> thanks a lot for the explanation. by the way, what does "ODR" mean?

"One Definition Rule". http://en.wikipedia.org/wiki/One_Definition_Rule

Side note, top-posting is discouraged in this forum. Better is to
bottom-post, or even better is to intersperse your reply with the
specific text you're replying to. In addition, you should remove quoted
signatures when replying as well.
 
M

Marcus Kwok

[top-posting redacted]

Hong,
Please do not top-post. Your replies should be placed below
*appropriately trimmed* quoted material. I have rearranged your post to
fix this.
thanks a lot for the explanation. by the way, what does "ODR" mean?

"ODR" means "One Definition Rule". Basically it says that function
definitions should be in one place only (usually in a .cpp file).

However, the "inline" keyword allows you to circumvent this when
necessary (for example, when the function definition is placed in a
header file that is included in multiple translation units). The caveat
is that the multiple definitions must all be the same (usually not a
problem if they are all coming from the same header file). If the
definitions do not match, then you have undefined behavior, and this is
something that the compiler does not necessarily have to tell you about.
 
N

Naresh Rautela

Traits is a useful template technique to simplfy the implementation of some
classes. however, I met some questions when I tried to understand and
implement it.
Following is an example of traits template with specializations (copied
from Nathan C. Myer's 1995 article):

template <class numT>
struct float_traits { };

struct float_traits<float> {
typedef float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
...
};

struct float_traits<double> {
typedef double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
...
};
Here are my questions,1) Are "float_traits" really a C++ class? I saw many
people call this as traits class. but I cannot find keyword "class" in the
definition.2) Is "static inline" keyword a requirement for member functions
within traits? what is the benefit of using static keyword here?any kind of
reply is appreciated.hong

The static function lets you have a construct like this
template<typename T>
typename float_traits<T>::float_type foo(const T& t)
{
typename float_traits<T>::float_type f =
float_traits<T>::epsilon(); //notice no need to create an object here
return f;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top