1

I have the following code, which doesn't compile:

class A {
public:
    typedef int A_t;
};

template<class T>
class IB {
public:
    typedef typename T::A_t B_t;
    virtual B_t get() = 0;
protected:
    B_t v;
    IB(B_t in) : v(in) { }
};

template<class T>
class B : public IB<T> {
public:
    B(B_t in) : IB<T>(in) { }   // error: expected ')' before 'in'
    B_t get() { return B_t(); } // error: B_t does not name a type
};

int main() {
    B<A> a(3);  // Errors here since int can't be converted to const B& or const B&&
                // Also because A is virtual, since A::get() doesn't compile
    return 0;
}

As far as I can understand the error messages, the main problem is that the typedef in IB for some reason isn't available in B.

If I add a line typedef IB<T>::B_t B_t; above the B constructor, the code compiles OK, but I don't understand why I need this. Shouldn't B_t be available through inheritance?

Also, the following doesn't work either:

B(IB<T>::B_t in) : IB<T>(in) { }
// etc, with qualified references to B_t

yields the same error messages.

What is the mechanism here that prevents me from using B_t it in B without the extra typedef?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402

0 Answers0