views:

282

answers:

5

I'm trying to define base class, which contains typedef's only.

template<typename T>
class A
{
public:
    typedef std::vector<T> Vec_t;
};


template<typename T>
class B : public A<T>
{
private:
    Vec_t v;  // fails - Vec_t is not recognized
};

Why in B I receive an error that Vec_t is not recognized and I need to write it explicitly?

typename A<T>::Vec_t v;
A: 

Because the compiler's not certain that Vec_t names a type. For example, A<T> might be specialized for T=int to not have that particular typedef.

Jesse Beder
A: 

You need to explicitly qualify the use of Vec_t because the compiler does not know where Vec_t comes from.

It cannot assume anything about the structure of A, since the class template A may be specialized. The specialization may include a Vec_t which is not a typedef, or it may not even include a member Vec_t at all.

Paul Baker
A: 

Vec_t is not a dependent name, and the compiler needs to know what it is without instantiating any templates (base class in this case). It is really no different from:

template <class T>
class X
{
    std::string s;
}

Here as well the compiler needs to know about std::string even if X is not instantiated, since the name does not depend on the template argument T (as far as the compiler can assume).

All in all, typedefs in a template base class seem rather useless for use in derived class. The typedefs are useful for the user, however.

UncleBens
A: 

There is something called dependent and nondependent names in case of templates.

If name depends on template parameter T its dependent name and others those do not depend on parameter T are independent names.

Here's the rule: the compiler does not look in dependent base classes (like A) when looking up nondependent names (like Vec_t). As a result, the compiler does not know they even exist let alone are types.

Compiler cannot assume that Vec_t is a type until it knows T because There is a potential specialization of A<T> where A<T>:: Vec_t is a is a data member

So the solution is use typename

 typename A<T>::Vec_t v;  ← good

I recommend you go through this http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

Xinus
+1  A: 

I believe that this question is duplicate, but I cannot find it now. C++ Standard says that you should fully qualify name according to 14.6.2/3:

In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

UPD: I found duplicate finally: here it is.

Kirill V. Lyadvinsky
By the way, it always bug me that I had to 're-typedef' everything... it's not pleasant, not pleasant at all.
Matthieu M.
btw you don't need all the template arguments and all, when qualifying. Because of the injected class name, it suffices to write `typename B::Vec_t`
Johannes Schaub - litb