Ответ 1
x = boost::none;
Как я могу "reset" / "unset" a 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
}
x = boost::none;
Один простой способ:
x = optional<int>(); //reset to default
Или просто:
x.reset();
Он уничтожает текущее значение, оставляя это неинициализированным (по умолчанию).