boost::mpl::for_each

Boost.MPL実は初めて使ったんですが



OK

#include <iostream>
#include <vector>
#include <string>
#include <boost/mpl/list.hpp>
#include <boost/mpl/for_each.hpp>

using namespace boost;

struct {
    template <class Type>
    void operator()(const Type&) const
    {
        std::cout << typeid(Type).name() << std::endl;
    }
} disp;


int main()
{
    typedef mpl::list<int, std::string, char, std::vector<int> > tl;

    mpl::for_each<tl>(disp);

    return 0;
}

NG

#include <iostream>
#include <vector>
#include <string>
#include <boost/mpl/list.hpp>
#include <boost/mpl/for_each.hpp>

using namespace boost;

template <class Type>
void disp(const Type&)
{
    std::cout << typeid(Type).name() << std::endl;
}


int main()
{
    typedef mpl::list<int, std::string, char, std::vector<int> > tl;

    mpl::for_each<tl>(disp); // エラー!...関数 テンプレート 'void disp(const Type &)' を関数引数として使用できません

    return 0;
}


関数テンプレートを渡すならこうしないといけないけど
mpl::for_each(disp);



Typeは関数呼び出しの度に毎回違うのでできません、と


関数オブジェクトのoperator()がメンバ関数テンプレートならを付けずに渡せるんですね