A C++ dependent name is a name that depends on a template argument. A non-dependent name does not depend on template arguments. The compiler resolves these two types of names at different points in time.
The issue of dependent and non-dependent names arises in templates. Two instantiations of the same class template may have different members, different types. Names that depend on a template argument are dependent names. Names that don't are non-dependent names. The compiler resolves non-dependent names at the point where a template is defined. Resolution of dependent names is deferred until the point where a template is instantiated.
This can lead to some strange compilation errors:
template <class T> class A
{
protected:
A(T v) : x(v) {}
T f() { return x; }
private:
T x;
};
template <class T> class B : public A<T>
{
public:
B() : A<T>(42) {}
T g () { return f(); } // Compilation error
};
The above fails to compile because of the non-dependent usage of f
in B::g()
. The solution is to turn that non-dependent name into a dependent name. Using a using declaration (using A<T>::f;
), using a qualified name (return A<T>::f();
) or explicitly using this
(return this->f();
) are three ways to turn the unqualified, non-dependent f
into a dependent name.