[英]Is it possible to use std::enable_if to select a member template specialization?
给出一个类声明
class A {
template <typename T> T foo();
};
我想专门A::foo
各类( int
,...)和类型的类(POD,非POD) T
。 不幸的是,我似乎无法使用std::enable_if
作为后者。 以下内容无法编译:
template <> int A::foo<int>(); // OK
template <typename T>
typename std::enable_if<is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
template <typename T>
typename std::enable_if<!is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
问题可能是由于std::enable_if<...>
这些东西是函数签名的一部分,而且我没有在A
声明任何这样的成员。 那么如何根据类型特征专门化模板成员呢?
我只是将它转发给一个结构,它可以很好地处理这个:
#include <type_traits>
#include <iostream>
template <typename T, typename = void>
struct FooCaller;
class A {
public:
template <typename T>
T foo() {
// Forward the call to a structure, let the structure choose
// the specialization.
return FooCaller<T>::call(*this);
}
};
// Specialize for PODs.
template <typename T>
struct FooCaller<T, typename std::enable_if<std::is_pod<T>::value>::type> {
static T call(A& self) {
std::cout << "pod." << std::endl;
return T();
}
};
// Specialize for non-PODs.
template <typename T>
struct FooCaller<T, typename std::enable_if<!std::is_pod<T>::value>::type> {
static T call(A& self) {
std::cout << "non-pod." << std::endl;
return T();
}
};
// Specialize for 'int'.
template <>
struct FooCaller<int> {
static int call(A& self) {
std::cout << "int." << std::endl;
return 0;
}
};
我认为没有理由在这里专注,重载功能似乎就足够了。
struct A
{
template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type foo()
{
std::cout << "integral" << std::endl;
return T();
}
template <typename T>
typename std::enable_if<!std::is_integral<T>::value, T>::type foo()
{
std::cout << "not integral" << std::endl;
return T();
}
}
在检查POD或没有POD时,您只有这两个选项,因此不需要更通用的功能(并且不允许,因为它是不明确的)。 你还需要更多吗? 在std::enable_if<std::is_same<int, T>::value, T>::type
的帮助下,您可以检查没有特化的显式类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.