Connecting Tech Pros Worldwide Forums | Help | Site Map

Error compiling template function within class template

uvts_cvs@yahoo.com
Guest
 
Posts: n/a
#1: Jul 22 '05
template <class T>
class foo
{
public:
template <class Tin>
T bar (Tin) {return T();}
};

class derived : public foo<derived>
{
};

Some compilers compile successfully, while another tells me that I'm
using the undefined class 'derived'.

Is this little chunk of code compliant with the ISO C++ standard or not?

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Error compiling template function within class template


uvts_cvs@yahoo.com wrote:[color=blue]
> template <class T>
> class foo
> {
> public:
> template <class Tin>
> T bar (Tin) {return T();}
> };
>
> class derived : public foo<derived>
> {
> };
>
> Some compilers compile successfully, while another tells me that I'm
> using the undefined class 'derived'.
>
> Is this little chunk of code compliant with the ISO C++ standard or not?[/color]

It probably is in that grey area, you know, twilight zone, where
all the templates live before they pop up into our minds...

The code is compliant. By the time you get around to initialising
a 'derived' object, the class 'derived' and therefore its base class
'foo<derived>' will have been completely defined. The compilers that
can't handle that kind of code aren't sophisticated enough to delay
generating of the code until it's actually needed.

Of course, you could have written

class derived : public foo<derived>
{
derived fubar() {
return foo<derived>::template bar(42);
}
};

which would cause an attempt to use 'foo<derived>' _before_ 'derived'
was completely defined. In that case the work-around is to place the
body of 'derived::fubar' outside the class definition.

V
Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Error compiling template function within class template


Victor Bazarov wrote in
news:FG3nd.11112$Ae.1903@newsread1.dllstx09.us.to. verio.net in
comp.lang.c++:
[color=blue]
> uvts_cvs@yahoo.com wrote:[color=green]
>> template <class T>
>> class foo
>> {
>> public:
>> template <class Tin>
>> T bar (Tin) {return T();}
>> };
>>
>> class derived : public foo<derived>
>> {
>> };
>>
>> Some compilers compile successfully, while another tells me that I'm
>> using the undefined class 'derived'.
>>
>> Is this little chunk of code compliant with the ISO C++ standard or
>> not?[/color]
>
> It probably is in that grey area, you know, twilight zone, where
> all the templates live before they pop up into our minds...
>
> The code is compliant. By the time you get around to initialising
> a 'derived' object, the class 'derived' and therefore its base class
> 'foo<derived>' will have been completely defined. The compilers that
> can't handle that kind of code aren't sophisticated enough to delay
> generating of the code until it's actually needed.
>
> Of course, you could have written
>
> class derived : public foo<derived>
> {
> derived fubar() {
> return foo<derived>::template bar(42);
> }
> };
>
> which would cause an attempt to use 'foo<derived>' _before_ 'derived'
> was completely defined. In that case the work-around is to place the
> body of 'derived::fubar' outside the class definition.
>[/color]

I tried it and the workaround wasn't needed, however with VC 7.1,
which exhibits the original problem, moving the member defenition
out of foo was required:

template < typename T > template < typename Tin >
T foo< T >::bar( Tin )
{
return T();
}

Incresing I'm thinking that MSVC 7.1 is pants.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Error compiling template function within class template


Rob Williscroft wrote:[color=blue]
> [...]
> Incresing I'm thinking that MSVC 7.1 is pants.[/color]

Rob, sorry for an off-topic question, but I am not familiar with
that idiom. Could you translate your sentence for me, please.
What does it mean "to be pants"? I found a verb "to [de]pants"
but that must not be it. Or is it? Thanks. -- Victor
Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Error compiling template function within class template


Victor Bazarov wrote in news:aA4nd.11122$Ae.10475
@newsread1.dllstx09.us.to.verio.net in comp.lang.c++:
[color=blue]
> Rob Williscroft wrote:[color=green]
>> [...]
>> Incresing I'm thinking that MSVC 7.1 is pants.[/color]
>
> Rob, sorry for an off-topic question, but I am not familiar with
> that idiom. Could you translate your sentence for me, please.
> What does it mean "to be pants"? I found a verb "to [de]pants"
> but that must not be it. Or is it? Thanks. -- Victor
>[/color]

LOL, sorry about that, it means rubbish.

http://www.proz.com/?sp=h&id=651556

I'll admit I thought it was a Bart (Simpsons) -ism, so genuine
International English, but apparently its very British.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Closed Thread