11

I'm new to boost::fusion and boost::mpl libraries. Could anyone please tell me the main difference between these two libraries?

Until now I used only fusion::vector and few other simple things. Now I want to use fusion::map or MPL::map but I don't know how to choose the right one.

I need map simple type to complicated type (type alisa). Currently I have following snippets and both works exactly I need to.

boost::fusion:

typedef boost::fusion::map<
    boost::fusion::pair<AliasNames::test1,int>,
    boost::fusion::pair<AliasNames::test2,double>,
    boost::fusion::pair<AliasNames::test3,float>
> TmapAssociations1;

typedef boost::fusion::result_of::value_at_key<TmapAssociations,AliasNames::test1>::type t;

boost::MPL:

typedef boost::mpl::map<
    boost::mpl::pair<AliasNames::test1,int>,
    boost::mpl::pair<AliasNames::test2,double>,
    boost::mpl::pair<AliasNames::test3,float>
> TmapAssociations2;

boost::mpl::at<TmapAssociations2,AliasNames::test1>::type t2;

Is there any difference between MPL and fusion? Are there any scenarios where one library is preferred over another one?

Thanks for reply.

Ludek Vodicka
  • 1,610
  • 1
  • 18
  • 33

2 Answers2

10

From the introduction of Fusion (the newer of the two):

STL containers work on values. MPL containers work on types. Fusion containers work on both types and values.

Choose MPL over fusion when doing pure type calculations. Once the static type calculation is finished, you can instantiate a fusion sequence (see Conversion) for the runtime part.

In your example, either way works. If you had more complex needs, maybe Fusion would do something extra for you (at runtime). But as it stands, I'd stick with MPL.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thank you for answer and the link. I have to miss this page. – Ludek Vodicka Jun 25 '11 at 20:49
  • Why do you choose MPL over fusion when doing pure type calculations? Is it because of performance? – Deqing Jun 04 '16 at 14:57
  • @Deqing: First, keep in mind I wrote this answer five years ago. Second, yes I do prefer MPL when doing pure type calculations, not because of performance but because I find (or at least, found) it easier to use. – John Zwinck Jun 05 '16 at 02:12
1

Boost.Fusion is there to bridge the gap between compile-time data structures and their runtime instances. It is basically a library of semantic-rich tuple-like data structures with associated algorithms.

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
Joel Falcou
  • 6,247
  • 1
  • 17
  • 34