如何设置一个boost ::可选回到未初始化的状态?
如何“重置”/“取消”一个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();
它破坏当前值,保留这个未初始化(默认)。