|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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; |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|