473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

question on template, traits class and static inline

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
5 2837
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: paidojoao-groups | last post by:
Given this code: #include <iostream> #include <string> class timestamp { public: timestamp() {
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
3
by: Raider | last post by:
I'm trying to create a base class who will contain generic algoritms and derived class(es) who will perform problem-specific things. I'm unable to use dynamic polymorphism, because base class know...
7
by: sheffmail | last post by:
I have the following code: template <class T> struct MyS { }; template<class T> inline const T& function(const std::locale& loc) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.