PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Problems with template arguments using G++ compiler
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problems with template arguments using G++ compiler

Réponse
 
LinkBack Outils de la discussion
Vieux 07/02/2008, 12h39   #1
pce@ecosimpro.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problems with template arguments using G++ compiler

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éponse avec citation
Vieux 07/02/2008, 14h25   #2
[rob desbois]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with template arguments using G++ compiler

On Feb 7, 12:39 pm, p...@ecosimpro.com wrote:
> 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;
  Réponse avec citation
Vieux 07/02/2008, 14h40   #3
pce@ecosimpro.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with template arguments using G++ compiler

On 7 feb, 15:25, "[rob desbois]" <rob.desb...@gmail.com> wrote:
> On Feb 7, 12:39 pm, p...@ecosimpro.com wrote:
>
>
>
>
>
> > 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;- 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
  Réponse avec citation
Vieux 07/02/2008, 15h12   #4
Pavel Shved
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with template arguments using G++ compiler

On 7 ÆÅ×, 15:39, p...@ecosimpro.com wrote:
> 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.
  Réponse avec citation
Vieux 07/02/2008, 15h39   #5
pce@ecosimpro.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with template arguments using G++ compiler

On 7 feb, 16:12, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
> On 7 ÆÅ×, 15:39, p...@ecosimpro.com wrote:
>
> > 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.


Thanks, this works properly!

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

Regards
  Réponse avec citation
Vieux 07/02/2008, 18h47   #6
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with template arguments using G++ compiler

pce@ecosimpro.com wrote:
> ...
> 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.

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15076 seconds with 14 queries