c++ - How negate std::is_integral for use in tag dispatch? -
i've been playing around tag dispatch, , following code works expect:
#include <type_traits> #include <iostream> void impl(std::true_type) { std::cout << "true\n"; } void impl(std::false_type) { std::cout << "false\n"; } template<typename t> void dispatch(t&& val) { impl(std::is_integral<typename std::remove_reference<t>::type>()); } int main() { dispatch(10); // calls impl(std::true_type) dispatch(""); // calls impl(std::false_type) }
but if want negate condition, i'm running trouble. thought throw "!
" code inside dispatch
,
impl(!std::is_integral<t>()); // added "!"
but won't compile.
what need code work?
you may instantiate std::integral_constant
constexpr value this:
impl(std::integral_constant<bool, !std::is_integral<t>::value>());
std::true_type
, std::false_type
aliases class. other way introduce metafunction this:
template <typename t> struct not_ : std::integral_constant<bool, !t::value> {};
and use (call) it:
impl(typename not_<std::is_integral<t>>::type());
or use similar boost::mpl
Comments
Post a Comment