来自tuple的构造函数参数 [英] Constructor arguments from tuple

查看:122
本文介绍了来自tuple的构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个模板,它由类类型和多个参数类型参数化。匹配这些类型的一组参数存储在元组中。如何将这些传递给类类型的构造函数?

Suppose I have a template which is parametrized by a class type and a number of argument types. a set of arguments matching these types are stored in a tuple. How can one pass these to a constructor of the class type?

在几乎C ++ 11代码中:

In almost C++11 code:

template<typename T, typename... Args>
struct foo {
  tuple<Args...> args;
  T gen() { return T(get<0>(args), get<1>(args), ...); }
};

... 构造函数调用没有固定长度?

How can the ... in the constructor call be filled without fixing the length?

我想我可以想出一些复杂的递归模板调用的机制,但我不能相信我

I guess I could come up with some complicated mechanism of recursive template calls which does this, but I can't believe that I'm the first to want this, so I guess there will be ready-to-use solutions to this out there, perhaps even in the standard libraries.

推荐答案

您需要一些模板元编程机制来实现。

You need some template meta-programming machinery to achieve that.

实现参数分派的最简单方法是利用包含扩展的编译时整数序列的表达式进行扩展。需要模板机制来构建这样的序列(还参见本答案末尾的注释以获得关于标准化这样的序列的提案的更多信息)。

The easiest way to realize the argument dispatch is to exploit pack expansion on expressions which contain a packed compile-time sequence of integers. The template machinery is needed to build such a sequence (also see the remark at the end of this answer for more information on a proposal to standardize such a sequence).

假设具有封装整数[M,N]和类(模板)的编译时范围的类(模板) index_range index_list ,它封装了整数的编译时列表,这是你将如何使用它们:

Supposing to have a class (template) index_range that encapsulates a compile-time range of integers [M, N) and a class (template) index_list that encapsulates a compile-time list of integers, this is how you would use them:

template<typename T, typename... Args>
struct foo
{
    tuple<Args...> args;

    // Allows deducing an index list argument pack
    template<size_t... Is>
    T gen(index_list<Is...> const&)
    {
        return T(get<Is>(args)...); // This is the core of the mechanism
    }

    T gen()
    {
        return gen(
            index_range<0, sizeof...(Args)>() // Builds an index list
            );
    }
};

这里是一个可能的实现 index_range index_list

And here is a possible implementation of index_range and index_list:

//===============================================================================
// META-FUNCTIONS FOR CREATING INDEX LISTS

// The structure that encapsulates index lists
template <size_t... Is>
struct index_list
{
};

// Collects internal details for generating index ranges [MIN, MAX)
namespace detail
{
    // Declare primary template for index range builder
    template <size_t MIN, size_t N, size_t... Is>
    struct range_builder;

    // Base step
    template <size_t MIN, size_t... Is>
    struct range_builder<MIN, MIN, Is...>
    {
        typedef index_list<Is...> type;
    };

    // Induction step
    template <size_t MIN, size_t N, size_t... Is>
    struct range_builder : public range_builder<MIN, N - 1, N - 1, Is...>
    {
    };
}

// Meta-function that returns a [MIN, MAX) index range
template<unsigned MIN, unsigned MAX>
using index_range = typename detail::range_builder<MIN, MAX>::type;

另请注意,有趣的提案 Jonathan Wakely 存在标准化 int_seq 类模板,这与我所说的 index_list 这里。

Also notice, that an interesting proposal by Jonathan Wakely exists to standardize an int_seq class template, which is something very similar to what I called index_list here.

这篇关于来自tuple的构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆