我已经定义了一个boost :: variant var,如下所示:
boost::variant<boost::blank, bool, int> foo;
Run Code Online (Sandbox Code Playgroud)
该变量在实例化但未初始化时具有type值boost::blank,因为它boost::blank是传递给模板化boost :: variant的第一个类型.
在某些时候,我想知道是否foo已初始化.我试过这个,但效果不好:
if (foo) //doesn't compile
if (foo != boost::blank()) //doesn't compile
if (!(foo == boost::blank())) //doesn't compile
Run Code Online (Sandbox Code Playgroud)
我认为值得注意的是,当foo初始化(例如,foo = true)时,它可以通过执行来"重置" foo = boost::blank();.
如何检查是否foo已初始化,即它的类型不同boost::blank?
我无法理解我一直在测试这个非常简单的列表的问题.想法是将项目放在列表中的位置i.我知道我通常不会使用列表来做到这一点.但是,这在我设置时有效item = 11,item = 12并且item = 13(分别是输出at position {1, 2, 3} there's the item {11, 12, 13}),但是当我设置时它不起作用item = 10,因为输出是at position 0 there's the item 6.
int main(void)
{
list<int> L;
L.push_back(10);
L.push_back(11);
L.push_back(12);
L.push_back(13);
int item = 10;
int pos;
list<int>::iterator it = moveToItem(item, L, pos);
cout << "at position " << pos << " there's the item " << *it;
}
list<int>::iterator moveToItem(int item, list<int> L, int& …Run Code Online (Sandbox Code Playgroud)