我一直在努力理解C ++选择模板的方式。即,请考虑以下代码示例:
template <typename R>
class Curious
{
public:
template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type = 33>
void test1() {}
template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type = 33>
void test1() {}
template <typename T, typename = typename std::enable_if<std::is_const<T>::value>::type>
void test2() {}
template <typename T, typename = typename std::enable_if<!std::is_const<T>::value>::type>
void test2() {}
template <typename std::enable_if<std::is_const<R>::value>::type * = nullptr>
void test3() {}
template <typename std::enable_if<!std::is_const<R>::value>::type * = nullptr>
void test3() {}
// works
template <typename T = void>
typename std::enable_if<std::is_const<R>::value, T>::type test4() {}
template <typename T = void>
typename std::enable_if<!std::is_const<R>::value, T>::type test4() {}
// also works
template <typename T = void, typename std::enable_if<std::is_const<R>::value, T>::type * = nullptr>
void test5() {}
template <typename T = void, typename std::enable_if<!std::is_const<R>::value, T>::type * = nullptr>
void test5() {}
}; // Curious
前两个函数(test1)工作正常(为什么?):
Curious<int> curious;
curious.test1<int>();
curious.test1<const int>();
而其余的则导致编译错误。关于函数test2,编译器声称我正在尝试创建一个副本:
error C2535: 'void Curious::test2(void)': member function already defined or declared
Here文件说:
一个常见的错误是声明两个仅在默认模板参数上有所不同的函数模板。这是非法的,因为默认模板参数不是函数模板签名的一部分,并且声明具有相同签名的两个不同函数模板是非法的。
所以情况似乎如此。但是,我没有看到与前两个函数有太大区别,前两个函数也有默认的模板参数。因此,我们对默认值(test1 - works)有一个默认类型(test2 - 不起作用)。关于它有什么规则吗?
在test3的情况下:
error C2039: 'type': is not a member of 'std::enable_if'Like in the first case this time the member function template has a default non-type parameter, but it depends on the class template parameter. Now SFINAE doesn't skip the wrong one (also not sure why).
在第四种情况下,SFINAE通过返回类型解析模板。但这些test4函数是否具有相同的签名?因为它们仅在返回类型上有所不同。
据我所知,在第五种情况下,添加额外参数使test5签名依赖于函数模板参数,因此SFINAE启动并且解析工作。
我对C ++如何处理这些模板感到很困惑。有人可以这么清楚这些事吗?
template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type>
void test1();
template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type>
void test1();
其中有明显不同的签名。template <typename T, typename> void test2();
template <typename T, typename> void test2();
哪些是明显相同的签名。R
在类中是固定的,你的enable_if
不依赖于函数的模板参数。int foo();
char foo(); // Illegal.
但
template <typename T> int foo();
template <typename T> char foo(); // legal, even it is not trivial to call
另外,std::enable_if<!std::is_const<R>::value, T>::type
依赖于模板参数T
,所以没关系。T
,所以也可以。