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
10
votes
3 answers

Why is it not required to use typename for dependent types in the following case?

I have been reading about removing reference of a type, here. It gives the following example: #include // std::cout #include // std::is_same template void print_is_same() { std::cout <<…
LernerCpp
  • 401
  • 1
  • 5
  • 13
9
votes
1 answer

Why can a dependent name be considered as complete even if the actual type is not defined until the very end

Consider this example: template void Yeap(T); int main() { Yeap(0); return 0; } template void YeapImpl(); struct X; template void Yeap(T) { YeapImpl(); // pass X to another template } template
felix
  • 2,213
  • 7
  • 16
7
votes
1 answer

Argument-dependent lookup of dependent names

This description on cppreference.com says that The lookup of a dependent name used in a template is postponed until the template arguments are known, at which time [...] ADL examines function declarations with external linkage that are visible from…
7
votes
1 answer

const dependent names returned from template functions, where does const go?

Suppose that I have a template function (e.g., foo), that returns a const dependent type. The options to qualify the return type as const is to either put const at the left of typename keyword: template const typename T::bar ^^^^^ foo(T…
101010
  • 41,839
  • 11
  • 94
  • 168
7
votes
2 answers

Should a class-member using-declaration with a dependent qualified-id be a dependent name?

Draft N3337 of the C++11 standard states in [namespace.udecl] A using-declaration introduces a name into the declarative region in which the using-declaration appears. Every using-declaration is a declaration and a member-declaration and so can be…
willj
  • 2,991
  • 12
  • 24
5
votes
1 answer

Different compiler behaviour when using alias as scope to get parent member

This code compiles fine on Clang and Visual C++ but not on GCC: #include template struct Test { Test(T &t) : _t(t) { } void method() { std::cout << _t.Internal::_value << "\n"; // Doesn't work on…
Johnmph
  • 3,391
  • 24
  • 32
5
votes
1 answer

What is the definition of "dependent name" in C++?

In C++, the concept of dependent names is important because: Such names are unbound and are looked up at the point of the template instantiation ... in both the context of the template definition and the context of the point of…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
5
votes
1 answer

C++ dependent name: Is this typename required?

In a.hpp I defined: #include namespace Board { template struct GroupNode { using PointType = std::pair; // ... }; } Then, in b.cpp I defined: #include "a.hpp" namespace Board { …
lz96
  • 2,816
  • 2
  • 28
  • 46
5
votes
2 answers

Visual C++ Compiler allows dependent-name as a type without "typename"?

Today one of my friends told me that the following code compiles well on his Visual Studio 2008: #include struct A { static int const const_iterator = 100; }; int i; template void PrintAll(const T & obj) { T::const_iterator…
hpsMouse
  • 2,004
  • 15
  • 20
4
votes
1 answer

std::basic_string::size_type causes compile error in C++20 mode

Here is a simple code that MSVC 2022 compiles in C++17 mode, but fails in C++20 mode: template void foo() { std::basic_string::size_type bar_size; //This fails to compile in C++20 } The error being reported in C++20 mode does…
Regus Pregus
  • 560
  • 3
  • 12
4
votes
1 answer

Is a diagnostic required for incorrect uses of non-dependent names in a template that is never instantiated?

Here's what the standard says about non-dependent names in a template definition: Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used. [Example 1: void g(double); void…
cigien
  • 57,834
  • 11
  • 73
  • 112
4
votes
2 answers

Specializing member function for non-template class in C++

I'm trying to specialize a template member function of a non-template class using a templatized parameter: #include class C { public: template void Foo( Container& ) { // ... } }; template
metal
  • 6,202
  • 1
  • 34
  • 49
4
votes
2 answers

Why can't I use QList::size_type as I would std::string::size_type? (template parameter error)

While researching an unsigned vs. signed integer comparison warning when declaring an iterator in my for loop, I read this: Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_type when comparing…
Zimano
  • 1,870
  • 2
  • 23
  • 41
4
votes
1 answer

Is this a bug in dependent name resolution in MSVC?

On cppreference.com, the following code is provided as example explaining dependent name resolution: #include void g(double) { std::cout << "g(double)\n"; } template struct S { void f() const { g(1); // "g" is a…
kinokijuf
  • 968
  • 1
  • 11
  • 33
4
votes
2 answers

Equivalent of "typename", to indicate that a dependant name is indeed a 'template template parameter'

We reduced a portion of code we cannot find the right syntax for to a minimal example. Let's assume the following definitions (worry not about the "why" ;) template class Element {}; template