uv******@yahoo.com wrote:
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