question on template, traits class and static inline

Discussion in 'C++' started by Hong Ye, Jun 1, 2007.

  1. Hong Ye

    Hong Ye Guest

    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
    Hong Ye, Jun 1, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 1, 2007
    #2
    1. Advertising

  3. Hong Ye

    Hong Ye Guest

    Hi Vic,

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

    "Victor Bazarov" <> wrote in message
    news:f3pvmk$ljp$...
    > 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
    >
    Hong Ye, Jun 1, 2007
    #3
  4. Hong Ye

    red floyd Guest

    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.
    red floyd, Jun 1, 2007
    #4
  5. Hong Ye

    Marcus Kwok Guest

    Hong Ye <> wrote:
    [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" <> wrote in message
    > news:f3pvmk$ljp$...
    >> 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
    Marcus Kwok, Jun 1, 2007
    #5
  6. On Jun 1, 12:26 pm, "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? 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;
    }
    Naresh Rautela, Jun 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    724
    Pete Becker
    May 4, 2005
  2. Sean
    Replies:
    4
    Views:
    651
    Rolf Magnus
    Apr 30, 2006
  3. Raider
    Replies:
    3
    Views:
    333
    Raider
    May 12, 2006
  4. Aarti

    Template traits

    Aarti, Jul 13, 2007, in forum: C++
    Replies:
    3
    Views:
    327
    Kai-Uwe Bux
    Jul 13, 2007
  5. ray
    Replies:
    1
    Views:
    1,291
    Robert Kern
    Jun 4, 2010
Loading...

Share This Page