Member template specialization problem

S

Severin Ecker

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
 
A

Alf P. Steinbach

* 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
 
S

Severin Ecker

Hi,

thanks for the quick reply.

* 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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top