Difference between boost::MPL and boost::fusion
Asked Answered
D

2

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.

Divulgence answered 25/6, 2011 at 20:36 Comment(0)
P
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.

Partlow answered 25/6, 2011 at 20:45 Comment(3)
Thank you for answer and the link. I have to miss this page.Divulgence
Why do you choose MPL over fusion when doing pure type calculations? Is it because of performance?Goniometer
@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.Partlow
D
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.

Dilettante answered 26/6, 2011 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.