简体   繁体   English

引用未在 Visual Studio 2017 中编译的迭代器

[英]reference to iterator not compiling in visual studio 2017

In my project in Visual c++ in visual studio 2015, I used references to iterators.在 Visual Studio 2015 的 Visual c++ 项目中,我使用了对迭代器的引用。

I updated to vs2017 today, but the compiler appears to have changed more than I thought.我今天更新到了 vs2017,但是编译器的变化似乎比我想象的要大。 Hopefully I can resolve most problems, but some I don't know why it would change.希望我能解决大多数问题,但有些我不知道为什么会改变。

For example, I used the snippet例如,我使用了片段

for (auto& it = stack.begin(); it != stack.end() /* not hoisted */; /* no increment */)
    {
        if (it->second->empty()) { stack.erase(it++); }    // or "it = m.erase(it)" since C++11
        else
        {
            auto obj = move(it->second);
            stack.erase(it);
            return move(obj);
        }
    }

where stack is a std::map<float, std::unique_ptr<StackableObj> .其中stackstd::map<float, std::unique_ptr<StackableObj>

It worked fine before, but in vs2017 does not compile, instead it produces the type error它之前工作正常,但在 vs2017 中无法编译,而是产生类型错误

Error   C2440   'initializing': cannot convert from 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>' to 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &'.

What do I do about this, or where do I look in order to know what has been changed?我该怎么办,或者我应该去哪里看才能知道发生了什么变化? Googling some terms didn't give any specific answers to what is going on.谷歌搜索一些术语并没有对正在发生的事情给出任何具体的答案。

Thanks in advance提前致谢

Non-const reference to an rvalue is not valid.对右值的非常量引用无效。 See this for more on the topic:有关主题的更多信息,请参阅内容:

By default, the compiler cannot bind a non-const or volatile lvalue reference to an rvalue.默认情况下,编译器无法将非常量或可变的左值引用绑定到右值。

The most optimal solution here is dropping & , since copying iterators is cheap.这里最好的解决方案是删除& ,因为复制迭代器很便宜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM