为变元元组构造参数堆栈

Construct parameter stack for variadic tuple

本文关键字:参数 堆栈 元组      更新时间:2023-10-16

我有一个包含可变元组的类,但需要自己构造参数堆栈。有人能告诉我怎么做吗?元组元素没有默认构造函数。

简化的代码如下所示:

#include <tuple>
struct base {};
template<class T>
struct elem
{
    elem(base*){}
    elem() = delete;
};
template<class... ARGS>
struct foo : base
{
    foo() : t( /* initialize all elems with this */) {}
    std::tuple<elem<ARGS>...> t;
};

int main()
{
    foo<int, double> f;
}

您可以执行以下操作:

template<class... ARGS>
struct foo : base
{
    foo() : t(elem<ARGS>(this)...) {}
    std::tuple<elem<ARGS>...> t;
};
#include <tuple>
template <typename... ARGS>
struct foo : base
{
    foo() : t(get_this<ARGS>()...) {}
    template <typename>
    foo* get_this() { return this; }
    std::tuple<elem<ARGS>...> t;
};

DEMO