检查类是否具有带TMP的复制构造函数



我一直在尝试一点SFINAE来确定泛型类型T是否有我可以使用的复制构造函数。这是我现在的位置。

template <bool statement, typename out>
struct Failable
{
    typedef out Type;
};
//This class is only used to insert statements that 
//could encounter substitution failure

template <typename O>
struct COPY
{
    template <typename T>
    typename Failable<true == sizeof(&T::T(const T&)), char>::Type copy(int)
    {}
    template <typename T>
    typename Failable<true, int>::Type copy(...)
    {}
};

然而,这也是我陷入困境的地方。&T::T(const T&)显然是一个无效的语句,因为我们不能为参数列表提供指向成员的指针,即使是p-m函数。我总是可以尝试指定某种void (T::*ptmf)(const T&) = &T::T,并希望它隐式地确定要放入指向成员函数的指针中的正确重载构造函数,但这也意味着构造函数有一个特定的返回类型,我必须指定它。

其他人有什么想法可以让我跋涉吗?(我还需要将类似的概念应用于检查分配运算符。)

提前感谢。

您可以使用std::is_copy_constructible和std::is _assignable。

您可以处理您的代码,只需要稍微修改一下。

template <bool statement, typename out>
struct Failable
{
     typedef out Type;
}; 
template <typename O>
struct COPY
{
     static O MakeO();
     template <typename U> // U and T are the same type
     static typename Failable<(sizeof U(MakeO())), char>::Type copy(int);
     template <typename U>
     static typename Failable<true, int>::Type copy(...);
     enum { value = sizeof(char) == sizeof( copy<O>(0) ) };
};

最新更新