I’m reading the documentation on std::integral_constant, and apparently it includes using type that refers to itself.
template <typename T, T Value>
struct integral_constant
{
using type = integral_constant<T, Value>;
// ...
};
What is this typedef used for?
To access it you already need to know the type, so I don’t see what benefit it brings.
>Solution :
From N1424:
An important use case for providing a type representation for the result of Boolean traits comes from the template metaprogramming, the discipline where class templates are treated as compile-time invocable entities (metafunctions), and where experience has shown that to build a consistent and coherent framework of well-interpolating components it’s crucially important for the metafunctions to provide a single common form of invocation [11]. Since it is possible to wrap an integral constant in a type, but not vice-versa, the implementation of boost type traits library was re-written to provide the result of Boolean trait templates through both their
static const bool valuemember and the nested type typedef exposing the corresponding specialization of an integral constanttypewrapper.
So if you had some overloaded function like:
void do_something(std::true_type);
void do_something(std::false_type);
Some type traits might look like:
template<typename T>
struct is_foo {
static const bool value = /*...*/;
typedef std::integral_constant<bool, value> type;
};
So you can still use do_something(typename is_foo<T>::type{}).
And for any "Boolean trait", you can always use do_something(typename Trait<T>::type{}), and that’s how you should write it generically, especially if Trait is given by some template-template parameter.