1

以下简化的类在从 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}; }
                ^
4

1 回答 1

0

将模板声明从:

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()

于 2014-07-16T16:43:50.510 回答