c++ - std::conditional compile-time branch evaluation -
compiling this:
template < class t, class y, class ...args > struct issame { static constexpr bool value = std::conditional< sizeof...( args ), typename std::conditional< std::is_same< t, y >::value, issame< y, args... >, // error! std::false_type >::type, std::is_same< t, y > >::type::value; }; int main() { qdebug() << issame< double, int >::value; return exit_success; }
gives me compiler error:
error: wrong number of template arguments (1, should 2 or more)
the issue issame< double, int >
has empty args
parameter pack, issame< y, args... >
becomes issame< y >
not match signature.
but question is: why branch being evaluated @ all? sizeof...( args )
false
, inner std:conditional
should not evaluated. isn't runtime piece of code, compiler knows sizeof..( args )
never true
given template types.
if you're curious, it's supposed variadic version of std::is_same
, not works...
you have error because type has correct when used template parameter.
may use template specialization solve issue, like:
#include <type_traits> template <typename ... ts> struct are_same; template <> struct are_same<> : std::true_type {}; template <typename t> struct are_same<t> : std::true_type {}; template <typename t1, typename t2, typename... ts> struct are_same<t1, t2, ts...> : std::conditional< std::is_same<t1, t2>::value, are_same<t2, ts...>, std::false_type >::type {}; static_assert(are_same<char, char, char>::value, "all type should identical"); static_assert(!are_same<char, char, int>::value, "all type should not identical");
Comments
Post a Comment