Inherited types using CRTP and typedef

The following code does not compile. I get an error: error C2039: 'Asub': not a member of 'C'

Can someone help me figure this out?

I ran the compiler VS2008 and 2010.

template <class T>
class B
{
    typedef int Asub;

public:
 void DoSomething(typename T::Asub it)
 {

 }
};

class C : public B<C>
{
public:
 typedef int Asub;

};

class A
{
public:
 typedef int Asub;

};


int _tmain(int argc, _TCHAR* argv[])
{
 C theThing;
 theThing.DoSomething(C::Asub());

 return 0;
}
+5
source share
1 answer

You are a little unfair to the compiler here. Cis incomplete without B<C>fully known and during processing B<C>, Cis still an incomplete type. There are similar streams on comp.lang.c ++. Moderated and comp.lang.c ++ .

, , , , :

struct C : B<C> {
    void f() { typedef typename C::Asub Asub; }
};

, :

template<class T, class Asub> struct B { /* ... */ };
class C : B<C, int> { /* ... */ };

... , :

template<class T, class Traits> struct B {
  void DoSomething(typename Traits::Asub it) {}
};

struct CTraits {
    typedef int Asub;
};

struct C : B<C, CTraits> {
    typedef CTraits::Asub Asub;    
};
+7

All Articles