The type-dependent type or value that is independent of the type

This post has been republished via RSS; it originally appeared at: Microsoft Developer Blogs.

Some time ago, we saw how to create a type-dependent expression that is always false. I noted that it feels weird creating a whole new type just to create a fixed false value. But maybe we can make it more useful by generalizing it. The unconditional_t alias template always represents the type T, and the unconditional_v variable template always represents the value v. Even though the resulting type or value is always the same, it is nevertheless a dependent type, and therefore the evaluation does not occur until template instantiation. We can use this to solve our "cannot static_assert(false) in a discarded statement" problem: The unconditional_t generalizes the alias template std::void_t<...>: Whereas std::void_t<...> always evaluates to void, the unconditional_t lets you pick the type that it resolves to. The unconditional_t also generalizes the template class std::type_identity<T>: Whereas std::type_identity<T> takes only one template type parameter, the unconditional_t lets you pass extra parameters, which are evaluated (for SFINAE) but otherwise ignored.  

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.