Member template specialization problem

Discussion in 'C++' started by Severin Ecker, Mar 9, 2009.

  1. Hi,

    I have the following piece of template code which works on VC7.1 and
    gcc4.1.1 without any warning or error, but CodeWarrior complains about
    an "illegal explicit template specialization".
    So I wanted to know whether my code is correct or if CodeWarrior
    rightfully yells at me.
    Btw, if I remove the inline at [1] I get an error due to a multiply
    defined symbol. I guess this is correct (?) but if so, can someone shade
    some light on this for me because I don't quite understand the why.

    many thanks in advance!


    template <bool P> struct GetRetVal
    template <class T>
    static T Get(void) {
    //some code here
    }
    };


    template <> struct GetRetVal<false> {
    template <class T>
    static T Get(void) {
    //some code here
    }
    };


    //codewarrior complains at line [2].
    //Oddly enough he shuts up if I add a second tempalte<>,
    //which breaks it with the other compilers.

    [0] template <>
    [1] inline
    [2] void GetRetVal<false>::Get<void>(void)
    [3] {
    [4] }


    best regards,
    severin
    Severin Ecker, Mar 9, 2009
    #1
    1. Advertising

  2. * Severin Ecker:
    >
    > I have the following piece of template code which works on VC7.1 and
    > gcc4.1.1 without any warning or error, but CodeWarrior complains about
    > an "illegal explicit template specialization".
    > So I wanted to know whether my code is correct or if CodeWarrior
    > rightfully yells at me.


    Code is technically correct except as noted below.


    > Btw, if I remove the inline at [1] I get an error due to a multiply
    > defined symbol. I guess this is correct (?) but if so, can someone shade
    > some light on this for me because I don't quite understand the why.


    A fully specialized function template is just an ordinary function with respect
    to linking, that is, it's a concrete function, so you really need that 'inline',
    yes.


    > template <bool P> struct GetRetVal


    Here you have a missing left brace.

    > template <class T>
    > static T Get(void) {
    > //some code here
    > }
    > };
    >
    >
    > template <> struct GetRetVal<false> {
    > template <class T>
    > static T Get(void) {
    > //some code here
    > }
    > };
    >
    >
    > //codewarrior complains at line [2].
    > //Oddly enough he shuts up if I add a second tempalte<>,
    > //which breaks it with the other compilers.
    >
    > [0] template <>
    > [1] inline
    > [2] void GetRetVal<false>::Get<void>(void)
    > [3] {
    > [4] }


    Removing the numbers and adding the missing the left brace the code compiles
    with Comeau Online (which is where you should check first of all).

    By the way, you probably don't need all this template stuff except if this is
    part of some type detection machinery.

    Also, using 'void' as formal parameter is a C-ism, noise to most C++ programmers.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, Mar 9, 2009
    #2
    1. Advertising

  3. Hi,

    thanks for the quick reply.


    Alf P. Steinbach wrote:
    > * Severin Ecker:
    >>
    >> I have the following piece of template code which works on VC7.1 and
    >> gcc4.1.1 without any warning or error, but CodeWarrior complains about
    >> an "illegal explicit template specialization".
    >> So I wanted to know whether my code is correct or if CodeWarrior
    >> rightfully yells at me.

    >
    > Code is technically correct except as noted below.
    >
    >
    >> Btw, if I remove the inline at [1] I get an error due to a multiply
    >> defined symbol. I guess this is correct (?) but if so, can someone
    >> shade some light on this for me because I don't quite understand the why.

    >
    > A fully specialized function template is just an ordinary function with
    > respect to linking, that is, it's a concrete function, so you really
    > need that 'inline', yes.
    >
    >
    >> template <bool P> struct GetRetVal

    >
    > Here you have a missing left brace.


    Ups yes, this was a copy & paste mistake.

    >
    >> template <class T>
    >> static T Get(void) {
    >> //some code here
    >> }
    >> };
    >>
    >>
    >> template <> struct GetRetVal<false> {
    >> template <class T>
    >> static T Get(void) {
    >> //some code here
    >> }
    >> };
    >>
    >>
    >> //codewarrior complains at line [2].
    >> //Oddly enough he shuts up if I add a second tempalte<>,
    >> //which breaks it with the other compilers.
    >>
    >> [0] template <>
    >> [1] inline
    >> [2] void GetRetVal<false>::Get<void>(void)
    >> [3] {
    >> [4] }

    >
    > Removing the numbers and adding the missing the left brace the code
    > compiles with Comeau Online (which is where you should check first of all).
    >
    > By the way, you probably don't need all this template stuff except if
    > this is part of some type detection machinery.


    Indeed it is something like that, so I can't really bail out there.

    >
    > Also, using 'void' as formal parameter is a C-ism, noise to most C++
    > programmers.


    Sorry for that one but thanks that you answered me nonetheless :)

    cheers,
    severin
    Severin Ecker, Mar 9, 2009
    #3
    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. Dave
    Replies:
    4
    Views:
    7,672
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,467
  3. BigMan
    Replies:
    1
    Views:
    525
  4. Joseph Turian
    Replies:
    4
    Views:
    589
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    335
    toton
    Feb 22, 2008
Loading...

Share This Page