在C ++中,你有一个修改变长元组的函数吗?

时间:2021-06-16 00:30:30

In C++0x I would like to write a function like this:

在C ++ 0x中我想写一个这样的函数:

template <typename... Types>
void fun(typename std::tuple<Types...> my_tuple) {
    //Put things into the tuple
}

I first tried to use a for loop on int i and then do:

我首先尝试在int i上使用for循环然后执行:

get<i>(my_tuple);

And then store some value in the result. However, get only works on constexpr.

然后在结果中存储一些值。但是,只能在constexpr上工作。

If I could get the variables out of the tuple and pass them to a variadic templated function I could recurse through the arguments very easily, but I have no idea how to get the variables out of the tuple without get. Any ideas on how to do that? Or does anyone have another way of modifying this tuple?

如果我可以从元组中获取变量并将它们传递给可变参数模板化函数,我可以很容易地通过参数递归,但我不知道如何在没有get的情况下从变量中获取变量。关于如何做到这一点的任何想法?或者有没有人有另一种修改这个元组的方法?

5 个解决方案

#1


4  

Since the "i" in

自从“我”进入

get<i>(tup)

needs to be a compile-time constant, template instantiation is used to "iterate" (actually recurse) through the values. Boost tuples have the "length" and "element" meta-functions that can be helpful here -- I assume C++0x has these too.

需要是一个编译时常量,模板实例化用于通过值“迭代”(实际上是递归)。 Boost元组具有“长度”和“元素”元函数,这在这里很有用 - 我假设C ++ 0x也有这些。

#2


3  

Boost.Fusion is worth a look. It can 'iterate' over std::pair, boost::tuple, some other containers and its own tuple types, although I don't think it supports std::tuple yet.

Boost.Fusion值得一看。它可以“迭代”std :: pair,boost :: tuple,一些其他容器和它自己的元组类型,虽然我认为它还不支持std :: tuple。

#3


0  

AFAICT, C++ tuples basically need to be handled with recursion; there don't seem to be any real ways of packing/unpacking tuples except using the typesystem's only variadic template handling.

AFAICT,C ++元组基本上需要用递归来处理;除了使用类型系统唯一的可变参数模板处理之外,似乎没有任何打包/解包元组的真实方法。

#4


0  

Take a look at section 6.1.3.4 of TR1, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

请参阅TR1的第6.1.3.4节,http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

get is defined for both const and non-const qualified tuples and returns the appropriate reference type. If you change your function declaration to the following:

get是为const和非const限定元组定义的,并返回适当的引用类型。如果将函数声明更改为以下内容:

template 
void fun(typename std::tuple& my_tuple) {
    //Put things into the tuple
}

Then the argument to your function is a non-const tuple and get will allow you to make the necessary assignments once you've written the iteration using the information provided in previous responses.

然后,函数的参数是一个非常量元组,一旦使用先前响应中提供的信息编写迭代,get将允许您进行必要的赋值。

#5


0  

Have a look at my answer here for an example of template recursion to unwind tuple arguments to a function call.

看看我的答案,这里有一个模板递归示例,用于解释函数调用的元组参数。

How do I expand a tuple into variadic template function's arguments?

如何将元组扩展为可变参数模板函数的参数?

#1


4  

Since the "i" in

自从“我”进入

get<i>(tup)

needs to be a compile-time constant, template instantiation is used to "iterate" (actually recurse) through the values. Boost tuples have the "length" and "element" meta-functions that can be helpful here -- I assume C++0x has these too.

需要是一个编译时常量,模板实例化用于通过值“迭代”(实际上是递归)。 Boost元组具有“长度”和“元素”元函数,这在这里很有用 - 我假设C ++ 0x也有这些。

#2


3  

Boost.Fusion is worth a look. It can 'iterate' over std::pair, boost::tuple, some other containers and its own tuple types, although I don't think it supports std::tuple yet.

Boost.Fusion值得一看。它可以“迭代”std :: pair,boost :: tuple,一些其他容器和它自己的元组类型,虽然我认为它还不支持std :: tuple。

#3


0  

AFAICT, C++ tuples basically need to be handled with recursion; there don't seem to be any real ways of packing/unpacking tuples except using the typesystem's only variadic template handling.

AFAICT,C ++元组基本上需要用递归来处理;除了使用类型系统唯一的可变参数模板处理之外,似乎没有任何打包/解包元组的真实方法。

#4


0  

Take a look at section 6.1.3.4 of TR1, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

请参阅TR1的第6.1.3.4节,http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

get is defined for both const and non-const qualified tuples and returns the appropriate reference type. If you change your function declaration to the following:

get是为const和非const限定元组定义的,并返回适当的引用类型。如果将函数声明更改为以下内容:

template 
void fun(typename std::tuple& my_tuple) {
    //Put things into the tuple
}

Then the argument to your function is a non-const tuple and get will allow you to make the necessary assignments once you've written the iteration using the information provided in previous responses.

然后,函数的参数是一个非常量元组,一旦使用先前响应中提供的信息编写迭代,get将允许您进行必要的赋值。

#5


0  

Have a look at my answer here for an example of template recursion to unwind tuple arguments to a function call.

看看我的答案,这里有一个模板递归示例,用于解释函数调用的元组参数。

How do I expand a tuple into variadic template function's arguments?

如何将元组扩展为可变参数模板函数的参数?