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

标签 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> >’

如有任何关于此错误的帮助,我们将不胜感激!

最佳答案

我会这样做:

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 .

关于c++ - 如何创建包含 n 次相同类型的类型列表(用于可变参数模板)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23985879/

相关文章:

c++ - 检测函数参数类型

c++ - 如何返回 std::string.c_str()

c++ - OpenCV 找不到库

c++ - 类型推导时间

php - 将文本字符串附加到 WooCommerce 产品标题

c++ - 不重复用作模板参数的类型

C++ Map 在尝试设置值时出现总线错误

C++ 线程 : what does join do exactly?

c++ - C++ 中的段错误在预分配缓冲区中创建的对象上调用虚方法

C++11 元编程 - 在编译期间查找枚举值(值包含间隙)