[ create a new paste ] login | about

Link: http://codepad.org/4NXH9McS    [ raw code | output | fork ]

uskz - C++, pasted on Mar 23:
#include <algorithm>
#include <iostream>
#include <string>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/construct.hpp>
#include <boost/lambda/lambda.hpp>

struct zombie
{
    virtual std::string name() const = 0;
    virtual ~zombie() { }
};

struct zombie0 : zombie
{
    virtual std::string name() const
    { return "zombie0"; }
};

struct zombie1 : zombie
{
    virtual std::string name() const
    { return "zombie1"; }
};

struct zombie2 : zombie
{
    virtual std::string name() const
    { return "zombie2"; }
};

namespace bll = boost::lambda;

boost::function<zombie*()> zombies_map[]
    =
{
    bll::new_ptr<zombie0>(),
    bll::new_ptr<zombie1>(),
    bll::new_ptr<zombie2>()
};

boost::function<zombie*(int)> const make_zombie
    = bll::bind(bll::constant(zombies_map)[bll::_1]);

int main()
{
    std::for_each(
        boost::make_counting_iterator(0),
        boost::make_counting_iterator(3),
        std::cout
            << bll::bind(&zombie::name, bll::bind(make_zombie, bll::_1))
            << '\n'
    );
}


Output:
1
2
3
zombie0
zombie1
zombie2


Create a new paste based on this one


Comments: