Questions tagged [dependent-name]

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.

63 questions
4
votes
2 answers

typename keyword and nested name specifier

struct A{}; template struct B { typename ::A a1; //(1) typename A a2; //(2): error }; int main(){return 0;} Why is the first case correct, but the second isn't? I don't understand the meaning of that restriction. And anyway,…
Denis
  • 2,786
  • 1
  • 14
  • 29
4
votes
1 answer

Is a function type dependent if it depends only on its own template parameters?

I came across an inconsistency in the way current C++ compilers (clang/gcc) determine whether a name is dependent. In the following example, A::f is dependent but ::f is not, resulting in an error when the latter is used. template struct…
willj
  • 2,991
  • 12
  • 24
4
votes
0 answers

Derived template-class access to base-class member-data: for dependent name

In the following long list of codes, please look for three places: // "this->" can be omitted before first data[0] and // Compile error, if "this->" is omitted before first data[0] and // likewise, "this->" is required. I don't know why…
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
4
votes
1 answer

Best match not found by ADL after point of instantiation. Is this UB?

Consider the following code, in which the location of the overloads of f causes some non-intuitive behaviour. The code compiles with no warnings in both Clang 3.4.1 and gcc 4.8. template struct A { static const int value =…
4
votes
1 answer

Clang access modifier order and decltype

I have been looking at creating a synchroniser helper template class which is based on Herb Sutter's ideas of a wrapper class in this talk This does not work in msvc as is (unless we remove the brace initialisation) but when brace initialisation is…
dirvine
  • 57,399
  • 2
  • 18
  • 19
3
votes
1 answer

error: type/value mismatch in template parameter list for std::variant

The following code doesn't work if class some_class is templated. So my guess is that I have to put the template specifier in front of something, but I don't really know where? I tried putting it in front of state::base and state::error types within…
glades
  • 3,778
  • 1
  • 12
  • 34
3
votes
0 answers

Template name disambiguation: g++ vs clang++

Premise: g++ and clang++ are known to be sometime discordant or not compliant on applying the rules for template disambiguation for dependent names. In this regard, the following code compiles under g++ but does not under clang++: template
JtTest
  • 47
  • 9
3
votes
1 answer

Compiler error when using integer as template parameter

What is wrong with the following piece of code? template struct A { template int foo() const { return N; } }; template struct B { int bar(const A& v) { …
Danvil
  • 22,240
  • 19
  • 65
  • 88
3
votes
2 answers

Unhide templated cast operator from templated base class

I have a templated base class with a templated conversion operator. I would like to unhide this templated conversion operator in a derived class (because of dependent-name lookup). template class A { public: template operator…
Stijn Frishert
  • 1,543
  • 1
  • 11
  • 32
3
votes
1 answer

Again on typename and template keywords

I have carefully read many answers concerning this topic, but nevertheless I cannot figure out EXACTLY when these two keywords ARE or AREN'T needed in the scope of a non-template function which is member of a nested template class. My reference…
GSi
  • 649
  • 3
  • 10
3
votes
1 answer

Specializing only nested template

I have following template: template struct First { template struct Second; }; Example specialization: template class D {}; template<> template<> struct First::Second { …
3
votes
1 answer

dependent types with variadic templates

Can you see anything wrong with this function declaration? template std::tuple foo(const Containers &...args); When I try to call it, like this: foo(std::list(),…
slyqualin
  • 297
  • 2
  • 12
3
votes
1 answer

Where does the C++ 98 standard specify that locally declared template names are not dependent?

According to this page: http://womble.decadent.org.uk/c++/template-faq.html#non-dependent "Non-dependent names are those names that are considered not to depend upon the template parameters, plus the name of the template itself and names declared…
willj
  • 2,991
  • 12
  • 24
2
votes
1 answer

Lookup of dependent qualified names

This program does not compile (error: 'foo' is not a member of 'N'): namespace N { // void foo(); } template void template_func(T t) { N::foo(t); } But if we uncomment the declaration of void foo();, it compiles. Demo. Both…
Dr. Gut
  • 2,053
  • 7
  • 26
2
votes
2 answers

Name resolution when a structure declaration is hidden by a variable

Let's consider the following demonstrative program. #include struct A { struct B { int b = 10; }; int B = 20; }; template struct C { void f() const { typename /*struct*/ T::B b; …
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335