What is the proper use of boost::fusion::push_back?
        Posted  
        
            by Kyle
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Kyle
        
        
        
        Published on 2010-05-15T20:07:02Z
        Indexed on 
            2010/05/15
            20:14 UTC
        
        
        Read the original article
        Hit count: 395
        
// ... snipped includes for iostream and fusion ...
namespace fusion = boost::fusion;
class Base
{
protected: int x;
public: Base() : x(0) {}
    void chug() { 
        x++;
        cout << "I'm a base.. x is now " << x << endl;
    }
};
class Alpha : public Base
{
public:
    void chug() { 
        x += 2;
        cout << "Hi, I'm an alpha, x is now " << x << endl;
    }
};
class Bravo : public Base
{
public:
    void chug() { 
        x += 3;
        cout << "Hello, I'm a bravo; x is now " << x << endl; 
    }
};
struct chug {
    template<typename T>
    void operator()(T& t) const
    {
        t->chug();
    }
};
int main()
{
    typedef fusion::vector<Base*, Alpha*, Bravo*, Base*> Stuff;
    Stuff stuff(new Base, new Alpha, new Bravo, new Base);
    fusion::for_each(stuff, chug());     // Mutates each element in stuff as expected
    /* Output:
       I'm a base.. x is now 1
       Hi, I'm an alpha, x is now 2
       Hello, I'm a bravo; x is now 3
       I'm a base.. x is now 1
    */
    cout << endl;
    // If I don't put 'const' in front of Stuff...
    typedef fusion::result_of::push_back<const Stuff, Alpha*>::type NewStuff;
    // ... then this complains because it wants stuff to be const:
    NewStuff newStuff = fusion::push_back(stuff, new Alpha);
    // ... But since stuff is now const, I can no longer mutate its elements :(
    fusion::for_each(newStuff, chug());
    return 0;
};
How do I get for_each(newStuff, chug()) to work?
(Note: I'm only assuming from the overly brief documentation on boost::fusion that I am supposed to create a new vector every time I call push_back.)
© Stack Overflow or respective owner