[英]How to return boost::fusion::vector<x,y,z> elements to add to a std::array<boost::fusion::vector<x,y,z>>?
我有一个std::array
和一个boost::fusion::vector<X, Y>
我想传递给func1()
。 此函数将向每个std::array
元素添加boost::fusion::vector<X, Y>
实例。
我必须使用fusion::fold()
这样我才能在fusion::vector<X,Y>
添加正确数量的元素,对吧?
所以我现在有这样的事情:
void func1(){
boost::fusion::vector<X,Y> my_vec;
std::array<boost::fusion::vector<X,Y> > my_array[10];
func2(my_vec, my_array);
}
void func2(boost::fusion::vector<X,Y> my_vec, std::array<boost::fusion::vector<X,Y> > my_array){
//THIS IS THE PART I AM UNSURE ABOUT
for(int k=0; k<10; k++){
//The first two parameters aren't so important- just included to show the idea
my_array[k] = boost::fusion::fold(my_vec, 1, some_struct);
}
}
//This part is irrelevant
struct some_struct
{
typedef int result_type;
template<typename T>
int operator()(int x, T& t) const
{
t = something(x);
//Not sure if this part needs to return a boost::fusion::vector<X, Y>
return x;
}
};
我不确定的部分是如何使用my_vec
的签名来创建多个boost::fusion::vector<X,Y>
实例并将它们返回以便我可以在func2()
添加到数组中。
有人可以建议吗?
编辑 - 刚发现我得到了fold()
错误的第一个参数,修改了我的问题。
我不知道我理解你的问题非常好所以首先让我们来解释一下什么折是,试图澄清。
通常(不仅仅用于融合)“折叠”采用两个参数的函数将其应用于向量的每个元素以及将函数应用于向量的前一元素的结果。 第一个元素被赋予初始值。
因此,如果我将函数定义为折叠为A f(A, B)
,则此函数的折叠将等效于(对于4个元素):
f(f(f(f(A_initial_value, B_0), B_1), B_2), B_3);
(大写前缀只是强制类型)
现在,更准确地说是融合折叠 。 它将在boost::fusion::vector<>
内的所有元素上折叠函数。 作为boost::fusion::vector<X, Y>
相当于std::tuple<X,Y>
它将在不同类型上调用f
:
A_final_value = f(A_initial_value, X_value), Y_value);
所以,当你这样做时:
my_array[k] = boost::fusion::fold(my_vec, 1, some_struct);
my_array[k]
将接收一个数值,并且由于您已将其定义为fusion::vector<X,Y>
,因此无法编译
所以有试图澄清fold
,我会说没有你的问题,但我承认,我不明白你所说的“正确的元素数量增加了的意思fusion::vector<X,Y>
”。
编辑:根据评论中的说法进行更新。 目标是在你的std::array<fusion::vector<int, int, int, int>>
生成一个斐波那契序列,例如行走array
每个vector
将以正确的顺序给出斐波纳契。
使用fusion
你必须通过迭代通过vector
的元素来传递状态,因此fold
是一个不错的选择。
这是我对此的建议(但是在第二个元素而不是第一个元素开始斐波纳契序列,因为我没有费心去处理特殊情况......对不起:)):
template <typename Value>
struct Generator
{
// Easier to read and mandatory for fold
typedef typename std::tuple<Value, Value> result_type;
// The function that generate the fibonacci and update the Value
result_type operator()(result_type previous, Value& elem) const
{
elem = std::get<0>(previous) + std::get<1>(previous);
return std::make_tuple(std::get<1>(previous), elem);
}
};
// Use template to be a bit more generic on array size and vector type
template <typename Vector, size_t array_size, typename Value>
void func2(std::array<Vector, array_size>& array, std::tuple<Value, Value> init)
{
// The state that will be fed across the fold function
auto previous = init;
for (auto& vect: array)
{
// Generate the fibonnaci value for every element of the vector starting
// from where the state is. The return value of fold is the new state
previous = boost::fusion::fold(vect, previous, Generator<Value>());
}
}
// Tool to print the vector
struct Printer
{
template <typename Elem>
void operator()(const Elem& elem) const
{
std::cout << elem << std::endl;
}
};
// Use template to be a bit more generic on array size and vector type
template <typename Vector, size_t array_size, typename Value>
void func1(std::tuple<Value, Value> init)
{
// Create the vector
std::array<Vector, array_size> array;
// FIll it with fibonacci
func2(array, init);
// Print it
for (auto vect: array)
{
boost::fusion::for_each(vect, Printer());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.