SFINAE enable_if explicit constructor

假装没事ソ 提交于 2019-12-05 02:41:50

Use

template <class...> struct null_v : std::integral_constant<int, 0> {};

and define the constructors as

template <typename T2,
          long = null_v<enable_if_t<SCheckEnable<U>::value, T2>>::value>
constexpr CClass(T2 v) : val(v) {};

template <typename T2,
          int = null_v<disable_if_t<SCheckEnable<U>::value, T2>>::value>
explicit constexpr CClass(T2 v) : val(v) {};

Making the argument dependent and actually instantiated. Demo.

[temp.deduct]/8:

If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments.

In your case the error occurs outside of any substitution, so that's not causing a deduction failure but rather makes your code ill-formed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!