C++0X 中的解包、函数应用和打包元组
在不使用 Boost 的情况下,在以下代码中编写 readvals 函数的最佳方法是什么?基本上,它应该获取一个元组,调用其元素的特定函数并再次将生成的结果作为元组返回。
是否有基于 C++0X 的 Functor 元组定义库?
template <class T>
struct A
{
A(T _val):val(_val){}
T ret() {return val;}
T val;
};
template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
//???
}
int main(int argc, char **argv)
{
A<int> ai = A<int>(5);
A<char> ac = A<char>('c');
A<double> ad = A<double>(0.5);
std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);
// assuming proper overloading of "<<" for tuples exists
std::cout << readvals<int,char,double>(at) << std::endl;
// I expect something like (5, c, 0.5) in the output
return 0;
}
我发现了关于 SO 的问题,部分解决了这个问题(元组解包、迭代元组元素等),但在我看来,与将所有这些解决方案放在一起相比,应该有一个更简单的解决方案。
What is the best way to write the readvals function in the following code without using Boost? Basically, it should get a tuple, call a specific function of it's elemets and return the generated results as a tuple again.
Is there any C++0X-based Functor definition library for tuples?
template <class T>
struct A
{
A(T _val):val(_val){}
T ret() {return val;}
T val;
};
template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
//???
}
int main(int argc, char **argv)
{
A<int> ai = A<int>(5);
A<char> ac = A<char>('c');
A<double> ad = A<double>(0.5);
std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);
// assuming proper overloading of "<<" for tuples exists
std::cout << readvals<int,char,double>(at) << std::endl;
// I expect something like (5, c, 0.5) in the output
return 0;
}
I have found questions on SO which deal partly with this problem (tuple unpacking, iterating over tuple elements, etc.), but it seems to me that there should be an easier solution compared to putting together all such solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您只想创建一个新元组,其内容是应用于旧元组内容的函数的结果?就像这样:
这是对的吗?为了回答这个问题,我正在窃取 @Luc Danton 的优秀元组索引器。从本质上讲,这种构造允许我们编写:
它是如何工作的:首先,
Indices
帮助器:现在对于应用程序:我们只需创建一个简单的固定函数
f
> 使其输入加倍。让我们将其应用于元组。这是一个硬连线函数,但您可以轻松地在
f
上对其进行模板化:最后是测试用例:
所有这一切都应归功于 Luc,他提出了自索引元组索引器。
If I understand correctly, you just want to make a new tuple whose contents are the results of a function applied to the contents of an old tuple? Like so:
Is this right? To answer that, I am stealing @Luc Danton's excellent tuple indexer. At the very heart, this construction allows us to write:
Here's how it works: First, the
Indices
helper:Now for the application: We just make a simple, fixed function
f
that doubles its input.Let's apply that to a tuple. Here's a hardwired function, but you can easily template that on
f
:Finally, the test case:
All credits for this should go to Luc, who came up with the self-indexing tuple indexer.
这是您要找的吗?我希望您至少可以使用它作为起点。处理元组和可变参数模板的方法比罗马的方法还要多......
Is this what you are looking for? I hope you can at least use it as a starting point. There are just more ways to approach working with tuples and variadic templates than there are ways that lead to rome...