Why a compiler can't deduce dependent names

Hello,
Could someone give me a dumbed down version of why a compiler can't deduce dependent names.

I kind of got the impression that:
1. The compiler can't get the template parameters from a function/class untill that function/class is initialized.
2 Inorder to initialize a function/class you need to figure out what it's dependent names are.
3. And of course you need the template parameter inorder to figure out what a dependent name is.

But I am not completely sure.
Last edited on
How would the compiler be able to know what std::vector<T>::value_type is if it doesn't know what T is yet?
Right, and the reason why it can't get T first and value_type second is you need value_type to get T? (a catch 22 situation?)
Item 1: The correct term is "instantiated".
Item 2: I'm not sure I understand what you mean by "dependent names".
Assuming that T is a type parameter, you have to instantiate a template with a specific type. When you write the template, you use T to represent the type that was passed when the template was instantiated. i.e. The compiler substitutes the type name for T.
When you write the template, you use T to represent the type that was passed when the template was instantiated. i.e. The compiler substitutes the type name for T.

And at that time it would also be possible to know what std::vector<T>::value_type is, but not before that.
Dependent names are names whose meaning depends on a template parameter:
https://eel.is/c++draft/temp.dep.general#def:expression,type-dependent

@jaffe15
The compiler doesn't solve equations when it does type deduction:
1
2
3
4
5
6
7
8
9
#include <type_traits>

template <typename T> 
  void f(typename std::type_identity<T>::type) {}
int main()
{
  // f(2); // wrong
  f<int>(2); // ok
}

For f(2) to compile, the compiler would need to solve the equation identity(T) = int for T, and it won't attempt to do that. See:
https://en.wikipedia.org/wiki/Unification_(computer_science)

I'd guess that C++ doesn't support this (at least) because it would need fundamental changes to support a solution.
For instance see the abstract of this paper:
https://www.moskal.me/pdf/msc.pdf
Last edited on
Topic archived. No new replies allowed.