8

I have to different vectors

mpl::vector<Type1, Type2...>
mpl::vector<Type3, Type4...>

I'd like to "concatenate" them to form:

mpl::vector<Type1, Type2, Type3, Type4...>

This would allow me to prepare vector templates and reuse them afterwards. There are different solutions to my problem, but this approach seems most appropriate to me.

Thanks...

3 Answers 3

7

The libaray native supported function boost::mpl::joint_view is probably a better choice. It is optimized and lazy-evaluated.

http://www.boost.org/doc/libs/1_55_0/libs/mpl/doc/refmanual/joint-view.html

1
  • Works like charm! And it does not explode the virtual memory, required for compilation. Thank you very much!
    – Kikosha
    Nov 20, 2013 at 14:48
3

Like this:

// include the appropriate headers
typedef mpl::vector<Type1, Type2> first_type;
typedef mpl::vector<Type3, Type4> second_type;
typedef mpl::copy<first_type::type, mpl::back_inserter<second_type> >::type concat_type;
4
  • I'm not sure, but I think that first_type and second_type are switched. If I'm not wrong with that you'll get vector<Type3,Type4,Type1,Type2>.
    – llonesmiz
    Oct 24, 2013 at 20:29
  • @cv_and_he Right, I just didn't give importance to the types' order.
    – Igor R.
    Oct 24, 2013 at 20:59
  • Thanks, this code does the job. Though in my case this method consumes a huge amount of memory at compile time...
    – Kikosha
    Oct 27, 2013 at 7:24
  • @Kikosha well, template metaprogramming requires a lot of ct-resources, especially with MSVC.
    – Igor R.
    Oct 27, 2013 at 9:16
2

You can use mpl::copy, which uses mpl::fold internally.

typedef mpl::vector<T0, T1> s0;
typedef mpl::vector<T2, T3> s1;
typedef mpl::copy<
    s1,
    mpl::back_inserter<s0>
>::type concatenated;

BOOST_MPL_ASSERT((
    mpl::equal<
        concatenated,
        mpl::vector<T0, T1, T2, T3>
    >
));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.