C++ 如何将 boost::optional 设置回未初始化状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8958348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:23:20 来源:igfitidea点击:
How to set a boost::optional back to an uninitialized state?
提问by Guy Sirton
How can I "reset"/"unset" a boost::optional
?
我怎样才能“重置”/“取消设置”一个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
}
回答by Benjamin Lindley
x = boost::none;
回答by Nawaz
One simple way is this:
一种简单的方法是:
x = optional<int>(); //reset to default
Or simply:
或者干脆:
x.reset();
It destroys the current value, leaving this uninitialized (default).
它会破坏当前值,使其未初始化(默认)。