Problems with template arguments using G++ compiler

P

pce

Hello,

we are having problems when trying to compile a template code using
GNU G++ compiler, however it compiles properly using Microsoft C++
compiler.

Given the following declaration

class foo
{
};

template< class T >
class myclass {
public:
static myclass *s_centl;
};

myclass<int> obj1; // OK in MS C++ and GNU
myclass<foo> obj2; // OK in MS C++ and GNU

template<int> myclass<int>* myclass<int>::s_centl = new
myclass<int>() ; // OK in MS C++ and GNU

template<foo> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in MS C++ but fails in GNU

the last statement produces the following error when using GNU C++
compiler:
"error 'class foo" is not a valid type for a template constant
parameter"

what I have to do to define properly this static member in GNU ?

Many thanks in advance
Pedro

pce1962
 
R

[rob desbois]

Hello,

we are having problems when trying to compile a template code using
GNU G++ compiler, however it compiles properly using Microsoft C++
compiler.

Given the following declaration

class foo
{

};

template< class T >
class myclass {
public:
static myclass *s_centl;

};

myclass<int> obj1; // OK in MS C++ and GNU
myclass<foo> obj2; // OK in MS C++ and GNU

template<int> myclass<int>* myclass<int>::s_centl = new
myclass<int>() ; // OK in MS C++ and GNU

template<foo> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in MS C++ but fails in GNU

the last statement produces the following error when using GNU C++
compiler:
"error 'class foo" is not a valid type for a template constant
parameter"

what I have to do to define properly this static member in GNU ?

Untested, but try this instead in the class declaration:
static myclass<T> *s_centl;
 
P

pce

Untested, but try this instead in the class declaration:
  static myclass<T> *s_centl;- Ocultar texto de la cita -

- Mostrar texto de la cita -

I have tried also your suggestion:
template< class T >
class myclass {
public:
static myclass<T> *s_centl;
};

... but it does not work either !

Any other idea how to solve it is wellcome.
Thanks
 
P

Pavel Shved

what I have to do to define properly this static member šin GNU ?

Remove foo from template keyword arguments, so instead of

template<foo> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in MS C++ but fails in GNU

it will look like

template<> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in both MS C++ and GNU :)

. Then change int with double, look at gnu errors and study template
specializations.
 
P

pce

Remove foo from template keyword arguments, so instead of

template<foo> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in MS C++ but fails in GNU

it will look like

template<> myclass<foo>* myclass<foo>::s_centl = new
myclass<foo>() ;// OK in both MS C++ and GNU :)

. Then change int with double, look at gnu errors and study template
specializations.

Thanks, this works properly!

Do you mean that it is a known problem in GNU Compiler?

Regards
 
A

Andrey Tarasevich

...
Do you mean that it is a known problem in GNU Compiler?
...

It is not. It is a problem with your code. What you are trying to do is
called "explicit specialization". (In this case you are trying to
perform explicit specialization of a static member of class template).
The syntax for explicit specialization always begins with 'template<>'
(note the empty '<>').

Your original code made no sense. I don't know why it compiled in MSVC
and partially compiler in GNU.

When the compiler saw the 'template<int> ...' part it assumed that you
are trying to define a completely different, new, unrelated template
with an unnamed template parameter of 'int' type. It was probably
supposed to choke on the rest of the "definition", but for some reason
did not.

When the compiler saw the 'template<foo> ...' part it, once again,
assumed that you are trying to define another completely different, new,
unrelated template with an unnamed template parameter of 'foo' type.
Since it is illegal to use class types as non-type template parameters,
GNU immediately complained. I don't know why MSVC didn't.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top