By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

question on template, traits class and static inline

P: n/a
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

Jun 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hong Ye wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '07 #2

P: n/a
Hi Vic,

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

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f3**********@news.datemas.de...
Hong Ye wrote:
>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jun 1 '07 #3

P: n/a
Hong Ye wrote:
[redacted]
>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).

[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.
Jun 1 '07 #4

P: n/a
Hong Ye <hy*@mathworks.comwrote:
[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.
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f3**********@news.datemas.de...
>Hong Ye wrote:
>>>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.

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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 1 '07 #5

P: n/a
On Jun 1, 12:26 pm, "Hong Ye" <h...@mathworks.comwrote:
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;
}

Jun 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.