小编Fer*_*nMG的帖子

如何确定boost :: variant变量是否为空?

我已经定义了一个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

c++ boost variant

10
推荐指数
2
解决办法
3672
查看次数

这个简单的std :: list中的bug在哪里?

我无法理解我一直在测试这个非常简单的列表的问题.想法是将项目放在列表中的位置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)

c++ stl list

1
推荐指数
1
解决办法
186
查看次数

标签 统计

c++ ×2

boost ×1

list ×1

stl ×1

variant ×1