以下简化的类在从 get() 返回值时会执行不同的操作,具体取决于该类是使用双精度还是数组作为模板参数:
#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}; }
^