templates - Disable Function when parameter type is void -
i have template class looking this:
template <typename t> constexpr bool is_value_passable_v = is_trivially_copyable_v<t> && sizeof(t) <= sizeof(void*) && !is_polymorphic_v<t>; template <typename b, typename t> using param_base_t = conditional_t<is_value_passable_v<b>, t, const t&>; template <typename t> struct param_d { using type = param_base_t<t, t>; }; template <> struct param_d<void> { using type = void; }; template <typename t> using param_t = typename param_d<t>::type; template <class tin> class cclass { public: static constexpr bool use_input_v = !is_same_v<typename tin::input_t, void>; using input_t = conditional_t<use_input_v, param_t<typename tin::input_t>, void>; enable_if_t<use_input_v> input(input_t i); };
the goal of code is, provde different input
functions different template paramters.
- a template parameter
input_t = int
should result invoid input(int i)
- a template parameter
input_t = std::vector
should result invoid input(const std::vector& i)
- a template parameter
input_t = void
should removeinput
function
compiling clang gives
/usr/bin/../include/c++/v1/type_traits:225:78: error: no type named 'type' in 'std::__1::enable_if<false, void>'; 'enable_if' cannot used disable declaration template <bool _bp, class _tp = void> using enable_if_t = typename enable_if<_bp, _tp>::type; ^~~
edit 1: after adding line
template <typename t> static constexpr bool use_input2_v = use_input_v;
and replacing function declaration with
template <typename t = void> enable_if_t<use_input2_v<t>> input(input_t i)
clang complains there's no matching member function call 'input'
:
note: candidate template ignored: substitution failure [with t = void]: non-type template argument not constant expression template <typename t = void> enable_if_t<use_input2_v<t>> input(input_t i); ~~~~~~~~~~~~ ^
edit 2: forgot mention, error comes 3 variants of template parameters.
edit 3: sample use case cclass
be
class cinput0 { using input_t = int; }; class cinput1 { using input_t = std::vector<int>; }; class cinput2 { using input_t = void; }; cclass<cinput0> in0; cclass<cinput1> in1; cclass<cinput2> in2; std::vector<int> = {1, 2, 3}; in0.input(3); in1.input(i); //in2.input() disabled
for sfinae`to work, needs working off dependent type, otherwise there no substitution failure. here example:
template <typename self = cclass<tin>> typename std::enable_if<self::use_input_v>::type input(typename self::input_t) { }
when member function template, compiler conditionally creates based on whether template parameters work. in original example, since whole class template, method not, compiler sees error member function class instantiated. using default template parameter trick need. want test considered dependent.
Comments
Post a Comment