Error compiling template function within class template

U

uvts_cvs

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?
 
V

Victor Bazarov

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?

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
 
R

Rob Williscroft

Victor Bazarov wrote in
in
comp.lang.c++:
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.

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.
 
V

Victor Bazarov

Rob said:
[...]
Incresing I'm thinking that MSVC 7.1 is pants.

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
 
R

Rob Williscroft

Victor Bazarov wrote in @newsread1.dllstx09.us.to.verio.net in comp.lang.c++:
Rob said:
[...]
Incresing I'm thinking that MSVC 7.1 is pants.

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

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.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top