以下简化的类在从 get() 返回值时执行不同的操作,具体取决于该类是被赋予 double 值还是数组作为模板参数:
#include "array"
#include "type_traits"
template<class T> class C
{
public:
T get(const int arg) {
return this->impl<T>(arg);
}
private:
template<class Out_T> typename std::enable_if<
std::is_same<Out_T, double>::value,
Out_T
>::type impl(const int arg) { return 1; }
template<class Out_T> typename std::enable_if<
std::is_same<Out_T, std::array<double, 3>>::value,
Out_T
>::type impl(const int arg) { return {1, 2, 3}; }
};
int main(int argc, char* argv[])
{
C<double> c1;
C<std::array<double, 3>> c2;
return c1.get(0) < c2.get(0)[1];
}
我应该如何编写 impl 的数组版本,以便支持数组中的任意数量的项?来自 g++-4.8.2 和 clang++-3.5 的错误没有帮助。我认为最接近的是:
template<
template<class...> class Out_T,
class... Args
> typename std::enable_if<
std::is_same<Out_T<Args...>, std::array<Args...>>::value,
Out_T<Args...>
>::type impl(const int arg) { return {1, 2, 3}; }
但 clang 仍然报错:
testi.cpp:8:20: error: no matching member function for call to 'impl'
return this->impl<T>(arg);
~~~~~~^~~~~~~
testi.cpp:31:28: note: in instantiation of member function 'C<std::__1::array<double, 3> >::get' requested
here
return c1.get(0) < c2.get(0)[1];
^
testi.cpp:13:7: note: candidate template ignored: disabled by 'enable_if' [with Out_T =
std::__1::array<double, 3>]
std::is_same<Out_T, double>::value,
^
testi.cpp:23:14: note: candidate template ignored: invalid explicitly-specified argument for template
parameter 'Out_T'
>::type impl(const int arg) { return {1, 2, 3}; }
^
最佳答案
更改模板声明:
template<
template<class...> class Out_T,
class... Args
>
到
template<
class Out_T
>
并将返回类型更改为:
typename std::enable_if<
detail::is_standard_array<Out_T>::value,
Out_T
>::type
其中 is_standard_array
是一个辅助模板,它返回一个 bool 值来确定类型是否为 std::array
:
namespace detail
{
template<class T>
struct is_standard_array : std::false_type { };
template<class T, std::size_t N>
struct is_standard_array<std::array<T, N>> : std::true_type { };
}
问题是 Args....
参数包没有提供给 impl()
的函数调用,因此编译器将其排除在外重载。您还可以为类 std::array
专门化 C
并适本地修改 get()
。
关于c++ - 当返回值是class或class<class>或class<class, class>等时如何使用enable_if?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24786077/