typename keyword
I used this keyword to specify that we are using a type, as opposed to an actual value.
For example, consider (NVM bad example)
- though it showcases how we ca have a variable be a “type” or a “value”
#include <iostream>
template <typename T, int Value>
struct A {
using t1 = T;
static constexpr int t2 = Value;
};
template <typename T>
struct A<T, 5> {
using t2 = T;
static constexpr int t1 = 6;
};
int main() {
A<int, 5> a;
typename A<int, 5>::t2 v = A<int, 5>::t1; // typename is actually not needed here, because we have a full specialization
std::cout << v << std::endl;
}
At the root of it
Structure templates can have different data members for each specialization.
The compiler needs to know in advance if a symbol within a structure is a type or a static member when the structure template depends on another template parameter.
The keyword typename placed before a structure template solves this ambiguity.
Below is a a better example:
template<typename T>
struct A {
using type = int;
};
template<typename R>
void g() {
using X = typename A<R>::type; // "type" is a typename or a data member depending on R
}