类模板构造函数中的SFINAE

SFINAE in class template constructors

本文关键字:SFINAE 构造函数      更新时间:2023-10-16

我正在尝试用模板和SFINAE制作一些东西,我是其中的初学者。我浪费了大量的时间去做每一件最简单的事情。你能帮我理解它是怎么工作的吗?

C<T,Ts…>取一个T参数,该参数要么是a<U>或B<U>,但在这两种情况下具有不同的行为。我无法向你展示我所做的一切。这是我觉得最不愚蠢的方式。

template<typename T> class A{
public: A(){} };
template<typename T> class B{
public: B(){} };
template<typename T> struct enable_if_A         {};
template<typename T> struct enable_if_A< A<T> > {typedef A<T> type;};
template<typename T> struct enable_if_B         {};
template<typename T> struct enable_if_B< B<T> > {typedef B<T> type;};
template<typename T,typename... Ts> class C{
public:
    C(typename enable_if_A<T>::type const &p){cout << "A" << endl;}
    C(typename enable_if_B<T>::type const &p){cout << "B" << endl;}
};
// ...
A<float> a;
B<float> b;
C<A<float> > ca(a); // error: no type named ‘type’ in ‘struct enable_if_B<A<float> >'
C<B<float> > cb(b); // error: no type named ‘type’ in ‘struct enable_if_A<B<float> >'

注意:我使用的是g++(Ubuntu/Linaro4.6.1-9ubuntu3(4.6.1。我应该升级吗?

感谢

编辑:更多细节,我也尝试(除其他外(:

template<typename T,typename... Ts> class C{
public:
    template<>
    C(typename enable_if_A<T>::type const &p){cout << "A" << endl;}
    template<>
    C(typename enable_if_B<T>::type const &p){cout << "B" << endl;}
};
//explicit specialization in non-namespace scope ‘class bcifs::C<T, Ts>’
//////////////////////////////////////////////////////////////////////////
template<typename T,typename... Ts> class C{
public:
    template<typename E=void>
    C(typename enable_if_A<T>::type const &p){cout << "A" << endl;}
    template<typename E=void>
    C(typename enable_if_B<T>::type const &p){cout << "B" << endl;}
};
// error: no type named ‘type’ in ‘struct enable_if_B<A<float> >'
//////////////////////////////////////////////////////////////////////////
template<typename T> struct enable_if_A         {};
template<typename T> struct enable_if_A< A<T> > {typedef void type;};
template<typename T> struct enable_if_B         {};
template<typename T> struct enable_if_B< B<T> > {typedef void type;};
template<typename T,typename... Ts> class C{
public:
    template<typename E=void>
    C(T const &p);
    C<typename enable_if_A<T>::type>(T const &p){cout << "A" << endl;}
    C<typename enable_if_B<T>::type>(T const &p){cout << "B" << endl;}
};
// error: invalid declarator before ‘(’ token
//////////////////////////////////////////////////////////////////////////
template<typename T> class C{
public:
    template<>
    C(T const &p,typename enable_if_A<T>::type * = 0){cout << "A" << endl;}
    template<>
    C(T const &p,typename enable_if_B<T>::type * = 0){cout << "B" << endl;}
};
// error: explicit specialization in non-namespace scope ‘class C<T>’
// error: no type named ‘type’ in ‘struct enable_if_B<A<float> >’
//////////////////////////////////////////////////////////////////////////
template<typename T> class C{
public:
    template<typename U>
    C(T const &p,typename enable_if_A<T>::type * = 0){cout << "A" << endl;}
    template<typename U>
    C(T const &p,typename enable_if_B<T>::type * = 0){cout << "B" << endl;}
};
// error: no type named ‘type’ in ‘struct enable_if_B<A<float> >’
// error: no matching function for call to ‘C<A<float> >::C(A<float>&)’
//////////////////////////////////////////////////////////////////////////
template<typename T> struct enable_if_A         {};
template<typename T> struct enable_if_A< A<T> > {typedef void type;};
template<typename T> struct enable_if_B         {};
template<typename T> struct enable_if_B< B<T> > {typedef void type;};
template<typename T> class C{
public:
    template <typename U>
    C(A<U> const & r, void* _ = 0);
};
template <typename T>
template <typename U>
C<T>::C<T>(A<U> const & r, typename enable_if_A<U>::type* _ = 0) {
    cout << "A" << endl;
}
// error: ISO C++ forbids declaration of ‘C’ with no type [-fpermissive]
// error: function template partial specialization ‘C<T>’ is not allowed
// error: no ‘int C<T>::C(const A<U>&, typename enable_if_A<U>::type*)’ member function declared in class ‘C<T>’
// C<T>::C<U>(... does the same

很抱歉,我从来没有运行过您的解决方案。我终于找到了:

// dummy-function-parameter-ed version :
template<typename T> class C{
public:
    template <typename U>
    C(A<U> const &r,typename enable_if<is_same<A<U>,T>::value>::type* = 0){cout << "A" << endl;}
    template <typename U>
    C(B<U> const &r,typename enable_if<is_same<B<U>,T>::value>::type* = 0){cout << "B" << endl;}
};
// and the dummy-template-parameter-ed version :
template<typename T> class C{
public:
    template<typename U,typename E = typename enable_if<is_same<A<U>,T>::value>::type>
    C(A<U> &r){cout << "A" << endl;}
    template<typename U,typename E = typename enable_if<is_same<B<U>,T>::value>::type>
    C(B<U> &r){cout << "B" << endl;}
};
template<typename T,typename... Ts> class C{
public:
    C(typename enable_if_A<T>::type const &p){cout << "A" << endl;}
    C(typename enable_if_B<T>::type const &p){cout << "B" << endl;}
};

这是错误的,但您已经知道:(原因是SFINAE只能在模板级别应用,但您正试图将其应用于模板的成员。也就是说,上面模板中的SFINAE只能应用于不同的C<T>类型,而不能应用于C<T>的构造函数。

为了能够将SFINAE应用于构造函数,您需要使构造函数成为一个模板。但在你的情况下,这将导致另一个限制。构造函数是特殊的函数,不能为其提供模板参数(即使构造函数是模板化的(,这意味着模板类型必须从调用位置推导出来。但是嵌套类型是不可推导的。。。

您可以通过更改构造函数的签名来绕过限制:

template <typename T>
template <typename U>
C<T>::C<U>(A<U> const & r, typename enable_if_A<U>::type* _ = 0) {
    // ...
}

在这种情况下,类C是一个模板,它有一个采用A<U>的模板化构造函数,该构造函数只能用于enable_if_A<U>::type确实是其类型的类型。类型可以通过第一个参数在调用地推导,推导出的类型U将在第二个参数上被替换。如果替换失败,模板化的构造函数将被丢弃。

上面的解决方案与C++03兼容。如果你有一个C++11编译器,你可以在不需要向构造函数添加额外参数的情况下完成同样的操作(即,不添加额外参数;如果我的语法正确,则不是100%:(:

template <typename T>
template <typename U, typename _ = typename enable_if_A<U>::type>
C<T>::C<U>(U const &) {...}

如果你不坚持使用SFINAE,你可以通过使用这样一个具有部分专业化的间接构造函数来轻松解决这个问题:

#include <iostream>
using namespace std;
template <typename T>
struct C
{
 T val;
 C() { init(); };
 void init() {};
};
template<> void C<string>::init() { val = "STR"; }
template<> void C<int>::init() { val = 5; }
int main () {
  C<int> x;
  C<string> y;
  cout << y.val << endl << x.val << endl;
}

此示例使用int和string,而不是您的类型A<U>和B<U>,但这应该没有什么区别(除了它使这个例子变得可编译之外(。