I would take the page from boost::hana and make the return value is_lvalue encoded the lvalue-ness of my argument as both a constexpr value and a type.
This allows you to do something like send tags without an additional template.
template<class T> constexpr std::is_lvalue_reference<T&&> is_lvalue(T&&){return {};}
the body of this function does nothing, and the value of the parameter is ignored. This allows it to be constexpr even for values ββother than constexpr.
The advantage of this technique can be seen here:
void tag_dispatch( std::true_type ) { std::cout << "true_type!\n"; } void tag_dispatch( std::false_type ) { std::cout << "not true, not true, shame on you\n"; } tag_dispatch( is_lvalue( 3 ) );
Not only is_lvalue return value is_lvalue available in the context of constexpr (since true_type and false_type have constexpr operator bool ), but we can easily choose overload depending on its state.
Another advantage is that the compiler makes it difficult to not embed the result. With a value of constexpr compiler can βeasilyβ forget that it is a true constant; with a type, it must first be converted to a bool order to be forgotten.
Yakk - Adam Nevraumont Mar 30 '16 at 14:34 2016-03-30 14:34
source share