简体   繁体   English

从 MSVC 2013 (C++11) 迁移到 MSVC 2019 (C++17) 时,std::vector::insert() 停止工作

[英]std::vector::insert() stopped working when migrating from MSVC 2013 (C++11) to MSVC 2019 (C++17)

I currently am migrating a large code base from我目前正在从

  • Visual Studio 2013 (v120) Visual Studio 2013 (v120)
  • C++11 C++11

to

  • Visual Studio 2019 (v142) Visual Studio 2019 (v142)
  • C++17 C++17

and now, my tests fail in strange places - I get index out of bounds crashes and other strange changes in behavior.现在,我的测试在奇怪的地方失败了 - 我的索引越界崩溃和其他奇怪的行为变化。 Upon digging I noticed that the following code:在挖掘时,我注意到以下代码:

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<int>> nestedVector;
    nestedVector.insert(nestedVector.begin(), {});
    std::cout << nestedVector.size() << " elements";
}

produces a one-element vector in VS2013 but an empty vector in VS2019.在 VS2013 中产生一个单元素向量,但在 VS2019 中产生一个空向量

Other ways of inserting其他插入方式

nestedVector.insert(nestedVector.begin(), std::vector<int>{});
nestedVector.insert(nestedVector.begin(), std::vector<int>());

work in both setups and properly add a new element.在两种设置中工作并正确添加新元素。 What is happening here?这里发生了什么?

As was already mentioned in the comments, in your original program the call of nestedVector.insert(nestedVector.begin(), {}) selects the overloaded method:正如评论中已经提到的,在您的原始程序中,调用nestedVector.insert(nestedVector.begin(), {})选择了重载方法:

iterator insert( const_iterator pos, std::initializer_list<T> ilist );

see https://en.cppreference.com/w/cpp/container/vector/insert .请参阅https://en.cppreference.com/w/cpp/container/vector/insert

Since the initializer list is empty, nothing is really inserted and the vector size is zero at the end.由于初始值设定项列表是空的,因此没有真正插入任何内容,并且最后向量大小为零。 It is the same behavior in all compilers, demo: https://godbolt.org/z/9nnET5ren在所有编译器中都是相同的行为,演示: https : //godbolt.org/z/9nnET5ren

Apparently, VS2013 did not implement this overload of insert method, so another one called:显然,VS2013没有实现这个insert方法的重载,所以另一个调用:

iterator insert( const_iterator pos, const T& value );

creating one element in the vector.在向量中创建一个元素。 This is wrong behavior.这是错误的行为。

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

相关问题 function 模板中的 Static 数组初始化 C++17 (MSVC 2019) - Static array initialization in a function template in C++17 (MSVC 2019) MSVC 2019 和 2022 编译器挂起并且在 c++17 和 c++20 中编译相对较大的 std::complex arrays 时不稳定 - MSVC 2019 and 2022 compiler hangs and is erratic compiling relatively large std::complex arrays in c++17 and c++20 带有MSVC 2013的C ++ 11类初始化程序 - C++11 in-class initializer with MSVC 2013 在C ++ 11中从C ++ 17重新实现std :: map :: try_emplace()? - Re-implementing std::map::try_emplace() from C++17 in C++11? 使用/ std:c ++最新版(或C ++ 17 / N4190)使用MSVC2015编译boost - Compiling boost with MSVC2015 with /std:c++latest (or C++17/N4190) 如果使用/ std:c ++ 17在MSVC2017 15.9.3中使用静态本地错误,lambda返回的值是多少? - How is value returned by lambda using a static local wrong in MSVC2017 15.9.3 with /std:c++17? 如何将解决方案文件中的所有项目更改为C ++ 17 MSVC? - How to change all projects in a solution file to C++17 MSVC? 在 MSVC 上的 c++17 中按索引迭代元组? - Iterating over a tuple by index in c++17 on MSVC? MSVC2015上的实验性C ++ 17文件系统:与权限不一致 - Experimental C++17 filesystem on MSVC2015: inconsistencies with permissions std :: map ::在C ++中插入更改17 - std::map::insert change in C++17
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM