Paste: foreach member c++0x

Author: erikc
Mode: c++
Date: Wed, 29 Sep 2010 18:33:29
Plain Text |
#include <iostream>

class Foo {
    public:
    int x;
    float y;

    template <typename Fn>
    void apply_to_members(Fn& fn) {
        fn(x, y);
    }
};


template <typename Fn>
struct Bar {
    Fn& fn;
    Bar(Fn& fn) : fn(fn) {}

    void operator()() {
    }
    template <typename T, typename... Params>
    void operator()(const T& t, Params... params) {
        fn(t);
        (*this)(params...);
    }
};

struct Baz {
    template <typename T>
    void operator()(const T& t) {
        std::cout << t << std::endl;
    }
};

int main()
{
    Foo f = { 3, 4.9 };
    Baz z;
    Bar<Baz> b(z);
    f.apply_to_members(b);
};

New Annotation

Summary:
Author:
Mode:
Body: