57

我怎样才能“重置”/“取消设置”一个boost::optional

optional<int> x;

if( x )
{
  // We won't hit this since x is uninitialized
}
x = 3;
if( x )
{
  // Now we will hit this since x has been initialized
}
// What should I do here to bring x back to uninitialized state?
if( x )
{
  // I don't want to hit this
}
4

2 回答 2

109
x = boost::none;

于 2012-01-22T01:54:06.150 回答
15

一种简单的方法是:

x = optional<int>(); //reset to default

或者简单地说:

x.reset(); 

它会破坏当前值,使此值未初始化(默认)。

于 2012-01-22T01:53:16.763 回答