施放boost :: fusion :: vector

时间:2012-12-21 10:45:26

标签: c++ boost boost-fusion

boost :: fusion :: vector的编号形式看起来像

template <class T1>
class vector1; 

template<class T1, class T2>
class vector2; 

可变形式看起来像

template<class T1 = boost::fusion::void_, class T2 = boost::fusion::void_>
class vector;

那么有没有办法在编译时将boost :: fusion :: vector从编号转换为可变形式?

2 个答案:

答案 0 :(得分:2)

你真的需要编译时演员吗?两者之间存在运行时转换,因此我无法真正看到需求:

vector2<int, char> a(13, 'b');
vector<int, char> b = a;
然而,我尝试过玩游戏。我对我的回答并不满意,但也许你可以努力找到更好的东西。

我希望能够使用一些元函数,但它似乎超出了我的能力。此外,使用这种方法,您需要多次定义它,因为您有不同的值。

也许更好的解决方案是首先转换为元组...

#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/container/vector/vector10.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/type_traits/remove_reference.hpp>
using namespace boost::fusion;

template<typename NumVec2>
struct cast {
    typedef typename result_of::at_c<NumVec2, 0>::type T1;
    typedef typename result_of::at_c<NumVec2, 1>::type T2;
    typedef vector<typename boost::remove_reference<T1>::type, typename boost::remove_reference<T2>::type > type;
};

int main(int, char**){
    vector2<int, char> a(13, 'b');
    typedef cast< vector2<int,char> >::type casted_t;
    casted_t other(10, 'f');
}

答案 1 :(得分:0)

所以我似乎有了答案

using numbered_vector = fusion::vector3<int, char, float>

   struct as_vvector
    {
        template <class T>
        class result;

        template <class Self, class ... Args>
        struct result <Self(Args...)> {
            using type = fusion::vector<
                typename std::remove_reference<Args>::type...>;
        };
    };

    using variadic_vector =
        typename fusion::result_of::invoke<
                    as_vvector, numbered_vector>::type;