Why is it not required to use typename for dependent types in the following case?
In a nutshell, you need typename
to ensure the compiler that
std::remove_reference<int>::type
really is a type. Lets consider some other template
template <typename T>
struct foo {
using type = int;
};
Here foo::type
is a type. But what if someone supplies a specialization along the line of
template <> struct foo<int> {
int type;
};
Now type
is not a type but an int
. Now when you use foo inside a template:
template <typanem T>
struct bar {
using type = typename foo<T>::type;
};
You have to ensure the compiler that foo<T>::type
really is a type, not something else, because only looking at bar
(and the primary template foo
) the compiler cannot know that.
However, in your main
the std::remove_reference<int>::type
does not depend on a template parameter, hence the compiler can readily check if it is a type.
The
type
s in thestd::remove_reference
traits are dependent types.
No, they are not dependent names here. The template arguments have been specified explicitly as int
, int&
and int&&
. Therefore, the types are known at this point.
On the other hand, if you use std::remove_reference
with a template parameter, e.g.
template <typename T>
void foo() {
print_is_same<int, typename std::remove_reference<T>::type>();
}
then you have to use typename
to tell that std::remove_reference<T>::type
is a type as your expression now depends on the template parameter T
.