I cannot find in c++ the difference between std::conditional< >::type
and std::conditional_t< >
.
When I compile
using A = typename conditional< true, int, char>::type;
using B = typename conditional_t< true, int, char>::type;
an error: expected nested-name-specifier gets out.
I was unable to use conditional
and nest, while conditional_t
seems to nest.
std::conditional_t
is just a helper alias. From cppreference:
template< bool B, class T, class F >
using conditional_t = typename conditional<B,T,F>::type;
On the same link you can also find the statement:
The behavior of a program that adds specializations for
std::conditional
is undefined.
The same is not true for type traits you write or that come with libraries. And then the crux is that the fact that some_trait::type
is a type is only a convention. Nothing in the language forbids you to specialize some_trait
such that type
is a static member of type int
whose value is 42
.
And thats why you need to disambiguate the member alias type
via typename
(see When is the "typename" keyword necessary?). std::conditional_t
on the other hand is a type alias. It can only be a type. Its a helper that makes the use of the trait more convenient.
std::conditional_t
(usually) has no member alias type
. It is an alias for the member alias type
of std::conditional
.