Boost.Fusion を使ってみた

Boost.Fusion は、MPL をバリバリ使える Tuple (でいいのかな)

#include <iostream>
#include <string>
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/include/vector.hpp>

using namespace std;
using namespace boost::fusion;

int main()
{
    boost::fusion::vector<int, double, string> t(3, 3.14, "Akira");

    cout << at_c<0>(t) << endl; // 3
    cout << at_c<1>(t) << endl; // 3.14
    cout << at_c<2>(t) << endl; // Akira

    return 0;
}

要素の書き換えはこうする

at_c<0>(t) = 0;


fusion 用のアルゴリズムも用意されている

#include <iostream>
#include <string>
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/algorithm.hpp>

using namespace std;
using namespace boost::fusion;

struct print {
    template <class T>
    void operator()(const T& value) const
    {
        cout << value << endl;
    }
};

int main()
{
    boost::fusion::vector<int, double, string> t(3, 3.14, "Akira");

    for_each(t, print());

    return 0;
}
3
3.14
Akira


もう Tuple いらないなー