【问题标题】:How to use boost::lambda to create new object for an existing pointer?如何使用 boost::lambda 为现有指针创建新对象?
【发布时间】:2013-02-19 01:10:55
【问题描述】:

我想要做的是 --> 在新线程中创建一个新对象。 比如:

Class* object = 0;
Arg arg;
boost::thread t( lambda::bind( object = lambda::new_ptr< Class >()( boost::ref( arg ) );

无法编译,正确的方法是什么?

【问题讨论】:

  • 提示:使用 Boost.Phoenix 而不是 Boost.Lambda——后者已被弃用多年。
  • 如果您可以选择 C++11,请执行 std::thread t([&amp;] { object = new Class(arg); } );
  • 谢谢大家,但我现在不能使用 C++11。我要试试凤凰……

标签: c++ boost boost-thread boost-lambda


【解决方案1】:

感谢 ildjarn,我尝试使用 boost::phoenix 并成功了,代码是:

Class* object = 0;
CArg arg0;
Arg arg1;

boost::thread t( boost::phoenix::val( boost::ref( object ) ) = boost::phoenix::new_< Class >( boost::cref( arg0 ), boost::ref( arg1 ) );

同样,来自 ildjarn,更好的代码是:

类*对象 = 0;

CArg arg0;

Arg arg1;

命名空间 phx = boost::phoenix;

boost::thread t( phx::ref( object ) = phx::new_( phx::cref( arg0 ), phx::ref( arg1 ) );

【讨论】:

  • 这应该是boost::thread t( phx::ref( object ) = phx::new_&lt; Class &gt;( phx::cref( arg0 ), phx::ref( arg1 ) );(给定namespace phx = boost::phoenix;)。值得注意的是,您应该使用命名空间 boost::phoenix 中的 ref/cref 而不是命名空间 boost
猜你喜欢
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2023-01-25
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多