如何创建包含n次相同类型的类型列表(对于可变参数模板)?

时间:2013-05-31 09:00:08

标签: c++ templates c++11 variadic-templates

我想要我的班级

template <class T, unsigned int n>
class X;

创建std::tuple,其中包含n次类型T。这有什么特别巧妙的方法吗?对于任意的可变参数模板类,还有一种很好的方法吗?

这是我先做的事情:

#include <tuple>

template <class, unsigned int, class>
struct simple_repeat_helper;

template <class T, unsigned int n, class... Args>
struct simple_repeat_helper<T, n, std::tuple<Args...>>
{
    typedef typename simple_repeat_helper<T, n-1, std::tuple<Args..., T>>::type type;
};

template <class T, class... Args>
struct simple_repeat_helper<T, 0, std::tuple<Args...>>
{
    typedef std::tuple<Args...> type;
};

template <class T, unsigned int n>
struct simple_repeat
{
    using type = typename simple_repeat_helper<T, n, std::tuple<>>::type;
};

但实际上,std::tuple我不需要这个,但是对于另一个行为相似的类。所以我认为我会创建一个更通用的版本:

template <class, unsigned int, template <class...> class, class>
struct repeat_helper;

template <class T, template <class...> class M, class... Args>
struct repeat_helper<T, 0, M, M<Args...>>
{
    typedef M<Args...> type;
};

template <class T, unsigned int n, template <class...> class M, class... Args>
struct repeat_helper<T, n, M, M<Args...>>
{
    typedef typename repeat_helper<T, n-1, M, M<Args..., T>>::type type;
};

template <class T, unsigned int n, template <class...> class M = std::tuple>
struct repeat
{
    using type = typename repeat_helper<T, n, M, M<>>::type;
};

我以为我可以像这样使用它:

repeat<double, 5, std::tuple>::type x = std::make_tuple( 1., 2., 3., 4., 5. ); 

但遗憾的是,compile由于:

而失败
ambiguous class template instantiation for ‘struct repeat_helper<double, 0u, std::tuple, std::tuple<double, double, double, double, double> >’

对此错误的任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:9)

我会这样做:

template<typename, typename>
struct append_to_type_seq { };

template<typename T, typename... Ts, template<typename...> class TT>
struct append_to_type_seq<T, TT<Ts...>>
{
    using type = TT<Ts..., T>;
};

template<typename T, unsigned int N, template<typename...> class TT>
struct repeat
{
    using type = typename
        append_to_type_seq<
            T,
            typename repeat<T, N-1, TT>::type
            >::type;
};

template<typename T, template<typename...> class TT>
struct repeat<T, 0, TT>
{
    using type = TT<>;
};

作为一个小测试:

#include <type_traits>
#include <tuple>

template<typename... Ts>
struct X { };

int main()
{
    repeat<double, 5, std::tuple>::type t = std::make_tuple(1., 2., 3., 4., 5.);
    static_assert(
        std::is_same<
            decltype(t),
            std::tuple<double, double, double, double, double>
        >::value, "!");

    repeat<double, 3, X>::type y;
    static_assert(
        std::is_same<decltype(y), X<double, double, double>>::value, "!");
}

最后,live example

答案 1 :(得分:8)

基于indices的解决方案:

template<typename Dependent, int Index>
using DependOn = Dependent;

// Assuming e.g. Indices<3> is indices<0, 1, 2>
template<typename T, int N, typename I = Indices<N>>
struct repeat;

template<typename T, int N, int... Indices>
struct repeat<T, N, indices<Indices...>> {
    // Can be an actual type-list instead of (ab)using std::tuple
    using type = std::tuple<DependOn<T, Indices>...>;
};

C++14 flavour

template<typename Dependent, std::size_t Index>
using DependOn = Dependent;

template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
struct repeat;

template<typename T, std::size_t N, std::size_t... Indices>
struct repeat<T, N, std::index_sequence<Indices...>> {
    using type = std::tuple<DependOn<T, Indices>...>;
};