4
// ... 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;
};
我该如何获得for_each(newStuff,chug())的工作?boost :: fusion :: push_back的正确用法是什么?
(注:我只是从overly brief documentation假设上的boost ::融合,我应该每次我打电话的push_back时间创建一个新的载体)
非常有帮助,谢谢。如何切换到实际对象会有好处,特别是如果它们不可复制? (不确定融合是否复制)。 – Kyle 2010-05-15 22:53:33
至少,它简化了内存管理(不需要在某处明确地删除,异常安全),并且不必担心可能的NULL指针(除非某些值是可选的)。 – 2010-05-16 11:23:52