Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between boost::MPL and boost::fusion

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.

like image 532
Ludek Vodicka Avatar asked Jun 25 '11 20:06

Ludek Vodicka


2 Answers

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.

like image 77
John Zwinck Avatar answered Nov 19 '22 14:11

John Zwinck


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.

like image 20
Joel Falcou Avatar answered Nov 19 '22 15:11

Joel Falcou