以下简化类在从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}; }但克朗仍在抱怨:
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}; }
^发布于 2014-07-16 16:43:50
将模板声明更改为:
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是一个助手模板,它返回一个布尔值,以确定该类型是否为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()提供,因此编译器将其排除为候选重载。您还可以为类C提供专门的std::array,并适当地修改get()。
https://stackoverflow.com/questions/24786077
复制相似问题