C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with template using base class template in GCC

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Walt Karas
Guest





PostPosted: Fri Aug 27, 2004 3:11 am    Post subject: Problem with template using base class template in GCC Reply with quote



The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX<AOrB>()); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(void);
};

template<>
inline int TheBase::baseX<A>(void) { return(1); }

template<>
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

But if I give 'baseX' a dummy parameter whose type is the type parameter
to the template, making explicit naming of the type paramenter in the
baseX call unecessary, it does compile:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX(AOrB())); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(AOrB aOrB);
};

template<>
inline int TheBase::baseX<A>(A a) { return(1); }

template<>
inline int TheBase::baseX<B>(B b) { return(2); }

Tpl<TheBase> aTpl;

Is there some rule in the standard that requires this behavior?
Or is this just a problem with the GCC compiler?

Another thing that surprised me was that GCC gives me an error if
I put the partial specializations of 'baseX' inside the declaration
of the class 'TheBase'.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gilles Rochefort
Guest





PostPosted: Sun Aug 29, 2004 11:15 am    Post subject: Re: Problem with template using base class template in GCC Reply with quote




Quote:
template
inline int TheBase::baseX<A>(void) { return(1); }

template
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

But if I give 'baseX' a dummy parameter whose type is the type parameter
to the template, making explicit naming of the type paramenter in the
baseX call unecessary, it does compile:

Well, I'm not sure to understand what you want to do here ????
But, from what you wrote, I guess that the compiler interpret the two
specialized functions as two more functions in the class TheBase.
Unfortunately, you don't explicitely make use of either class A or
either class B,so the compiler can't decide wich one to choose.


Quote:
class TheBase
{
public:

template<typename AOrB
int baseX(AOrB aOrB);
};

template
inline int TheBase::baseX
template
inline int TheBase::baseX<B>(B b) { return(2); }

Tpl<TheBase> aTpl;


Here, as you provide two functions with different input arguments,
compiler can now assign two functions to TheBase that are :
int TheBase::baseX(A a);
int TheBase::baseX(B b);

This is a common overloading rule. Remember, you can't overload function
with the same argument.

Quote:
Another thing that surprised me was that GCC gives me an error if
I put the partial specializations of 'baseX' inside the declaration
of the class 'TheBase'.

Because of c++ doesn't allow template specializations inside a class ...
More exactly specialized template should be associated with a namespace
scope.

Hope, all this will help !
Probably if you tell more about what you want to do exactly, I should help
you a bit more.

Best regards,
Gilles Rochefort









[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Vine
Guest





PostPosted: Sun Aug 29, 2004 11:16 am    Post subject: Re: Problem with template using base class template in GCC Reply with quote



Walt Karas wrote:

Quote:
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:

class A { };
class B { };

template <typename Base
class Tpl : protected Base
{
public:
template int x(void) { return(baseX ^^^^^^^^^^^^^^^^^^^^^^

(See below)
Quote:
};

class TheBase
{
public:

template<typename AOrB
int baseX(void);
};

template
inline int TheBase::baseX
template
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

[snip]

I have had to change quite a lot of code of this kind to compile with
gcc-3.4. According to gcc-3.4, the syntax for your call in int Tpl:Mad() to
a method in the templated base class is:

int x(void) { return(Base::baseX<A0rB>()); }
^^^^^^
In other words, you need to specify that baseX() is a member of the
templated base class. Whether that is required by the standard I know not,
but I expect it is right. gcc-3.4 is a lot stricter than gcc-3.2/3.3.

You have set follow-ups to a group I do not read, so I will not see any
response (not a very good idea).

Chris.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul
Guest





PostPosted: Sun Aug 29, 2004 10:40 pm    Post subject: Re: Problem with template using base class template in GCC Reply with quote

<snip>
Quote:

Because of c++ doesn't allow template specializations inside a class ...
More exactly specialized template should be associated with a namespace
scope.

Are you sure about that?

class Test{
public:
template<typename T>void foo(T){}
template<>void foo(double){}
};

The above compiles ok , not saying that means it's right, but are you
ssaying this is not allowed? Is the namespace for foo here not Test?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gilles Rochefort
Guest





PostPosted: Tue Aug 31, 2004 10:13 am    Post subject: Re: Problem with template using base class template in GCC Reply with quote



Quote:
class Test{
public:
template<typename T>void foo(T){}
template<>void foo(double){}
};

Yes, as I said this not allowed ...
The above code don't compile on a recent gcc ( I tested on a 3.3.2 )
I guess that gcc becomes lesser and lesser permissive ..

So, now you have to write the following code to get a chance to compile.

class Test{
public:
template<typename T>void foo(T){}
};

template<>void Test::foo(double){}

Gilles.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sharad Kala
Guest





PostPosted: Tue Aug 31, 2004 7:24 pm    Post subject: Re: Problem with template using base class template in GCC Reply with quote


"Paul" <invisiblepaul (AT) hotmail (DOT) com> wrote

Quote:
snip

Because of c++ doesn't allow template specializations inside a class ...
More exactly specialized template should be associated with a namespace
scope.

Are you sure about that?

class Test{
public:
template<typename T>void foo(T){}
template<>void foo(double){}
};


Yes, he is correct. The relevant section in standard is 14.7.3/2: "An
explicit specialization shall be declared in the namespace of which the
template is a member, or, for member templates, in the namespace of which
the enclosing class or enclosing class template is a member."

-Sharad




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.