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
2
votes
2 answers

refer to a non-dependent name without specifying template parameter

Consider the following: template struct Foo { typedef void NonDependent; typedef typename T::Something Dependent; } I would like to refer to NonDependent without specifying any template parameter, as in…
L. Bruce
  • 170
  • 7
2
votes
1 answer

Nested classes are dependent types in class templates?

Consider the following: template struct T { struct T1 { struct T2 { }; }; /*typename*/ T1::T2 m; }; Without typename, compilation fails as T2 is considered a dependent name, and thus not a type. After…
TSmith
  • 405
  • 3
  • 10
2
votes
0 answers

Invoking templated member of templated class from within a templated function, with explicit parameter

template struct foo { template void f(int i){} }; template void bar() { foo m; m.f<1>(1); // line A } int main(){ bar<1>(); foo<1> n; n.f<1>(1); // line B …
Museful
  • 6,711
  • 5
  • 42
  • 68
2
votes
2 answers

nested template class and a function in the global namespace

I am working on a template Graph data structure, which is an STL vector of GraphNode objects. I defined the GraphNode class nested inside the Graph class and when I invoke the overloaded insertion operator for the GraphNode object inside the…
2
votes
1 answer

Using variables from parent class

I've been trying to use template to implement stack. And my question is how do I use the variables from the parent class in this situation? In this case my compile error is: 'top, a, size' was not declared in this scope. template
Archy
  • 33
  • 6
2
votes
1 answer

Is it required to qualify a dependent name with typename if it is used as a template argument?

template void f(mapT& m, const K& k, const V& v) { pair p = m.insert(make_pair(k, v)); } MSVC accepts this code with no errors or warnings. What does the standard have to say about this? Are…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2
votes
1 answer

more dependent types with variadic templates

This follows yesterday's question, where I gave some C++ code that Visual Studio 2013 couldn't handle, and @galop1n kindly provided a workaround, which worked perfectly for that case. But now I've gone a tiny bit further and Visual Studio is giving…
slyqualin
  • 297
  • 2
  • 12
1
vote
0 answers

C++ primer 5th ed templates dependent and non-dependent names

In C++ Primer 5th edition Chapter16 on templates: "It is up to the provider of a template to ensure that all names that do not depend on a template parameter are visible when the template is used. Moreover, the template provider must ensure that the…
Maestro
  • 2,512
  • 9
  • 24
1
vote
2 answers

What does "typename iterator_traits::difference_type" mean?

This is the example-implementation of the count algorithm from https://devdocs.io/cpp/algorithm/count_if: template typename iterator_traits::difference_type count(InputIt first, InputIt last, const T& value) { …
izaguirrejoe
  • 117
  • 8
1
vote
1 answer

Are unqualified names of non-static data members with dependent types dependent

Dependent names are not clearly defined in the C++ standard, so it leaves a lot to be desired in terms of determining what a dependent name is, which leads me to this question: Are unqualified names of non-static data members with dependent types…
Krystian S
  • 1,586
  • 7
  • 23
1
vote
1 answer

Computing the type of a function pointer

Consider the following: template struct S { typedef M< &T::foo > MT; } This would work for: S SW; where Widget::foo() is some function How would I modify the definition of struct S to allow the following instead: S
unshul
  • 269
  • 3
  • 16
1
vote
1 answer

Compiler confuses name of an (unrelated) template with method name

I have a compile error in the following code. It seems that the compiler interprets class method set as a template which - at first glance - is completely unrelated to my code. #include #include using namespace std; template…
Michael
  • 7,407
  • 8
  • 41
  • 84
0
votes
1 answer

Is there an equivalent of "typename" for template types?

We have a template member function of the form: template