Boost mpl for each function and free

Why this code does not compile:

#include 
#include 
#include 

using namespace std;
using namespace boost;

template    // specific visitor for type printing
static void print_type(T t)
    {
        std::cout << typeid(T).name() << std::endl;
    }

typedef mpl::vector s;
int main ()
{
    mpl::for_each(print_type());
}

I wonder – how to make boost mpl for_each work with free functions from same class?

As stated you need a functor.

The code below includes an additional wrap template that allows the print functor to cope with references.

#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace boost;

template 
struct wrap {};

struct print_type
{
    template
    void operator()( wrap ) const
    {
        cout << typeid(T).name() << "n";
    }
};

typedef mpl::vector s;

int main ()
{
    mpl::for_each<s, wrap >(print_type());
    return 0;
}

Note: this code based on examples in the book ‘C++ Template Metaprogramming by David Abrahams and Aleksy Gurtovoy

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Boost mpl for each function and free

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录