I am quite sure I have read the reason why the compiler cannot cope with this code somewhere on SO, but, after a few hours of searching, I still can’t find it. Here is the relevant code:
#include <iostream>
template <typename T>
class base
{
};
class derived : base<derived::myStruct>
{
public:
struct myStruct
{
};
};
int main ()
{
return 0;
}
The problem is that the parser first tries to generate the base<derived::myStruct> specialization before parsing derived, and, thus, I get this error: “error C2065: ‘myStruct’ : undeclared identifier”. As a silly trick, I noticed that VS2010 stops complaining if I pre-declare struct myStruct; just above class derived. In my opinion, myStruct should be bound inside derived and this code should throw the same error:
#include <iostream>
template <typename T>
class base
{
};
struct myStruct;
class derived : base<derived::myStruct>
{
public:
struct myStruct
{
};
};
int main ()
{
return 0;
}
Update: gcc-4.5.1 is able to throw the expected error, so, I guess the above is a bug in VS2010…
A workaround is to declare
myStructoutside ofderived.In more complex cases where
derivedis a template class,derived_myStructwould too be a template class, with the same template paramerters (or just a subset), and you would pass those through.