Guy*_*ton 53 c++ boost 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
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*ley 95
x = boost::none;
Run Code Online (Sandbox Code Playgroud)
Naw*_*waz 12
一个简单的方法是:
x = optional<int>(); //reset to default
Run Code Online (Sandbox Code Playgroud)
或者干脆:
x.reset();
Run Code Online (Sandbox Code Playgroud)
它会破坏当前值,使其保持未初始化状态(默认值).
| 归档时间: |
|
| 查看次数: |
23183 次 |
| 最近记录: |