本文介绍了如何填写的boost ::融合::向量在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,道歉的相似到我的previous问题 href=\"http://stackoverflow.com/questions/13094535/iterating-over-boost-fusionvector\">,但我不要以为我问了正确的事情。

Firstly, apologies for the similarity to my previous question here, but I don't think I asked the right thing.

我有一个方法:

template <typename T>
void some_method( T &t)
{...}

这需要一个类型融合::矢量&lt; T1,T2,T3,...,TN&GT; 要在运行时确定 - 例如矢量&lt;整型,双&GT; 在一个电话,矢量&lt; INT,双,INT方式&gt; 在另一

which takes a type fusion::vector<T1, T2, T3, ..., Tn> to be determined at runtime - e.g. vector<int, double> in one call and vector<int, double, int> in another.

我要的东西,如动态填补这个载体:

I want to fill this vector dynamically with something like:

int blah = 5;
for(int i = 0; i<size(t); i++){
at_c<i>(t) = blah;
}

这行不通,因为at_c需要一个常量

This doesn't work since at_c expects a const.

我尝试过其他的东西(见previous问题),但仍然无法工作,如何实现这一目标。

I've tried other stuff (see the previous question) but still can't work out how to achieve this.

任何帮助非常AP preciated!
谢谢你。

Any help much appreciated!Thanks.

推荐答案

由于@Mankarse指定正确,你不能在融合容器>为循环,这是因为融合容器都是关于元组,每个元素可以有与其他元素不同的类型,即通过融合迭代容器中的所有函数实际上是一对夫妇的功能,通常为模板或重载函数。因此,为了初始化融合 vector容器你应该有多种功能(或仅仅是将是一个模板编译成多个类或函数)都从矢量和状态变量,可以为每个调用增加)访问该向量(或至少是一个迭代器。所以,你有2个选择:

As @Mankarse specified correctly, you can't use fusion containers in a for loop and that's because fusion containers are all about tuple and each element may have different type from other elements, all functions that iterate through a fusion container are actually a couple of functions and usually implemented as template or overloaded functions. So in order to initialize a fusion container from a vector you should have multiple functions (or simply a template that will be compiled to multiple classes or functions) that all have access to that vector(or at least an iterator from the vector and an state variable that can increased for each call). So you have 2 options:

1)使用boost ::融合::方面:

1) Use boost::fusion::fold:

template< class StdIteratorT >
struct initialize_fusion_container_from_std_iterator {
    typedef StdIteratorT    result_type;

    template< class T >
    StdIteratorT operator()( StdIteratorT i, T& val ) {
        val = *i;
        return ++i;
    }
};
void use_fold_demo() {
    int p1[] = {4, 5, 6};
    fusion::vector<int, double, int> fv;
    std::vector<int> sv2( p1, p1 + _countof(p1) );
    fusion::fold( fv, sv2.begin(),
    initialize_fusion_container_from_std_iterator<std::vector<int>::iterator>() );
}

2)编写递归调用本身与容器的下一个项目的函数(记住这个函数的语法如下递归函数,但它不是递归在所有):

2) Write a function that recursively call itself with next item of the container(remember syntax of this function is like recursive functions but it is not recursive at all):

// this will be called when we reach end of the fusion container(FIBeginT==FIEndT)
template< class FIBeginT, class FIEndT, class StdIteratorT >
void set_fusion_iterator( FIBeginT b, FIEndT e, StdIteratorT i, boost::mpl::true_ )
{
}
// this will be called when FIBeginT != FIEndT
template< class FIBeginT, class FIEndT, class StdIteratorT >
void set_fusion_iterator( FIBeginT b, FIEndT e, StdIteratorT i, boost::mpl::false_ )
{
    *b = *i;
    set_fusion_iterator( fusion::next(b), e, ++i,
        fusion::result_of::equal_to<
            typename fusion::result_of::next<FIBeginT>::type, FIEndT >() );
}

void recursive_function_demo() {
    typedef fusion::vector<int, double, int>    my_fusion_vector;

    int p1[] = {1, 2, 3};
    std::vector<int> sv1( p1, p1 + _countof(p1) );
    fusion::vector<int, double, int> fv;
    set_fusion_iterator( fusion::begin(fv), fusion::end(fv), sv1.begin(),
        fusion::result_of::equal_to<
            typename fusion::result_of::end<my_fusion_vector>::type,
            typename fusion::result_of::begin<my_fusion_vector>::type>() );
}

正如你所看到第二种情况要复杂得多,但如果你了解它的逻辑,你可以用它做融合容器任何东西,所以选择都是你的! !

As you see second case is much more complicated, but if you understand its logic you can use it to do anything with fusion containers, so the choice is all yours!!

这篇关于如何填写的boost ::融合::向量在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:32