Reputation: 732
Numbered form of boost::fusion::vector looks like
template <class T1>
class vector1;
template<class T1, class T2>
class vector2;
etc.
And variadic form looks like
template<class T1 = boost::fusion::void_, class T2 = boost::fusion::void_>
class vector;
So is there any way to cast boost::fusion::vector from numbered to variadic form at compile time?
Upvotes: 4
Views: 519
Reputation: 732
So it seems that I have an answer
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;
Upvotes: 0
Reputation: 9711
Do you actually need a compile-time cast ? There is a run-time conversion between both, so I can't really see the need :
vector2<int, char> a(13, 'b');
vector<int, char> b = a;
I tried however to play around. I'm not satisfied with my answer, but maybe you can work on it to find something better.
I hoped to be able to use some meta function, but it seems beyond my abilities. Furthermore, with this approach you need to define it as many times as you have different values.
Maybe a better solution would be to first convert to a tuple…
#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');
}
Upvotes: 2