c++ - SFINAE enable_if 显式构造函数

标签 c++ templates c++11 sfinae enable-if

我正在尝试通过 enable_if 在显式和隐式转换构造函数之间切换。

我的代码目前看起来像

#include <type_traits>
#include <cstdint>

enum class enabled {};

template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type;

template <std::intmax_t A> struct SStruct
{
    static constexpr std::intmax_t a = A;
};

template <typename T> struct SCheckEnable : std::integral_constant<bool, T::a == 0>
{
};

template <typename U, typename T> class CClass
{
    public:
        template <typename T2, enable_if_t<SCheckEnable<U>::value, enabled>...> constexpr CClass(T2 v) : val(v) {};
        template <typename T2, disable_if_t<SCheckEnable<U>::value, enabled>...> explicit constexpr CClass(T2 v) : val(v) {};

    private:
        T val;
};

int main()
{
    CClass<SStruct<0>, double> a = 1;                             // should use implicit constructor
    CClass<SStruct<1>, double> b = CClass<SStruct<1>, double>(1); // should use explicit constructor
}

enable_if 中的true 依赖于模板参数U

如果我尝试使用 g++ 4.9.1 和启用 --std=c++11 编译这个最小的例子,我会得到以下错误

sfinae.cpp: In substitution of ‘template<bool B, class T> using disable_if_t = typename std::enable_if<(! B), T>::type [with bool B = true; T = enabled]’:
sfinae.cpp:13:52:   required from here
sfinae.cpp:7:95: error: no type named ‘type’ in ‘struct std::enable_if<false, enabled>’
 template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type;
                                                                                               ^
sfinae.cpp:19:68: error: prototype for ‘constexpr CClass<U, T>::CClass(T2)’ does not match any in class ‘CClass<U, T>’
 template <typename U, typename T> template <typename T2> constexpr CClass<U, T>::CClass(T2 v) : val(v)
                                                                    ^
sfinae.cpp:13:77: error: candidates are: template<class U, class T> template<class T2, int ...<anonymous> > constexpr CClass<U, T>::CClass(T2)
   template <typename T2, disable_if_t<true, enabled>...> explicit constexpr CClass(T2 v);
                                                                             ^
sfinae.cpp:12:67: error:                 template<class U, class T> template<class T2, enabled ...<anonymous> > constexpr CClass<U, T>::CClass(T2)
   template <typename T2, enable_if_t<true, enabled>...> constexpr CClass(T2 v);
                                                                   ^

知道如何根据此处的参数 U 在显式和隐式构造之间进行选择吗?

最佳答案

使用

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

并将构造函数定义为

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) {};

使参数依赖并实际实例化。 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.

在您的情况下,错误发生在任何替换之外,因此这不会导致推导失败,而是会使您的代码格式错误。

关于c++ - SFINAE enable_if 显式构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26949290/

相关文章:

c++ - Boost.asio 服务器-客户端。连接两台电脑

templates - Play 2.0 - 在模板中映射 `map` 或 `for-yield` 结果

c++ - 应该如何以 C++11 风格初始化这个成员 vector ?

c++ - 如何根据模板变量参数多次扩展语句

java - 如何在 JNI 中访问从 C++ 返回 java.lang.String 的 Java 方法的返回值?

c++ - Qt 中的多线程有问题吗?

c++ - 在类定义中使用 extern decl 说明符进行编程

c++ - C++-有什么方法可以为性能和封装构造常量数据?

c++ - 这种继承有什么问题?

c++ - 使用成员函数启动线程