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从数字形式转换为可变形式呢?
发布于 2012-12-24 06:31:38
你真的需要一个编译时转换吗?两者之间有一个运行时转换,所以我看不出有什么必要:
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');
}发布于 2012-12-24 20:35:50
所以看起来我有一个答案
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;https://stackoverflow.com/questions/13988631
复制相似问题