c++ - Implementing is_convertible trait for function and array types -


i have (thanks @xeo) relatively simple is_convertible implementation:

template<typename from, typename to> struct is_convertible { private:   static void foo(to);   template<typename f>   static auto test(int) -> decltype(foo(declval<f>()), void(), true_type{});   template<typename>   static auto test(...) -> false_type; public:   static constexpr bool value = decltype(test<from>(0))::value;   constexpr operator bool() { return value; } }; 

which works "normal" stuff, , augmented cv-qualified void versions (which believe should compactable current 20 specializations void), pretty useful.

the problem when try array , function types:

typedef void function(); typedef char array[1]; 

then these tests fail:

static_assert(!is_convertible<function, function>(), ""); static_assert(!is_convertible<function&, function>(), ""); static_assert(!is_convertible<function*, function>(), ""); static_assert(!is_convertible<function*const, function>(), ""); static_assert(!is_convertible<array, const array>(), ""); static_assert(!is_convertible<array&, array>(), ""); static_assert(!is_convertible<array&, const array>(), ""); static_assert(!is_convertible<const array&, const array>(), ""); static_assert(!is_convertible<char*, array>(), ""); static_assert(!is_convertible<char*, const array>(), ""); static_assert(!is_convertible<const char*, const array>(), ""); 

can above trait augmented work these types well, or need specializations, , how these look?


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -