c++ - Exception safety of std::vector implementation shipped with Visual Studio 2015 -
consider following program:
#include <iostream> #include <vector> #include <type_traits> struct x { int state; x() : state(0) {}; x(int x) : state(x) {}; x(x const& y) noexcept : state(1) {} x(x&& y) noexcept(false) : state(y.state != 8 ? 2 : throw 1) { y.state = 0; } x& operator =(x const&) = default; x& operator =(x&&) = default; }; int main() { std::vector<x> a; a.reserve(2u); a.emplace_back(6); a.emplace_back(8); try { // #### std::cout << 'a' << a.front().state << a.back().state; } catch (...) { std::cout << 'b' << a.front().state << a.back().state; } return 0; }
at position // ####
different statements may employed, each giving different output on g++/clang++ versus msvc.
statement g++ msvc expected 1 a.resize(10u, x{}); a11 b08 a11 2 x b; a.push_back(b); a11 b08 a11 3 a.emplace_back(1); a11 b08 a11
so, why expect a11
in cases here?
1: a.resize(10u, x{});
[vector.capacity], 16/17 of n4140 , n4606 void resize(size_type sz, const t& c);
requires:
t
shallcopyinsertable
*this
.remarks: if exception thrown there no effects.
thus, if compiler must guarantee full rollback in case of exception possible ("no effects") must either copy elements or can move if move noexcept because moving has immediate effect on elements cannot rolled (which necessary if moving throw in turn not case noexcept move constructors).
therefore, i'd expect a11
output (or b68
if copy constructor throws).
2: x b; a.push_back(b);
3: a.emplace_back(1);
[vector.modifiers], 1 of n4140 , n4606 say:
if exception thrown while inserting single element @ end ,
t
copyinsertable
oris_nothrow_move_constructible<t>::value
true
, there no effects.
again standard requires "no effect" in case of exception when type "copyinsertable" (x copyinsertible). -> move_if_noexcept!
my interpretation of mandates a11
expected output (or in other terms, conformant std::vector shouldn't move elements unless moving noexcept or copying impossible).
i tried find information on vs sl found blog post 'vs 2015 update 2’s stl c++17-so-far feature complete' on vs blog suggests vs standard library implementation "feature complete" don't whether supposed claim standard compliance.
note: doubt i'm first spot vs incorrectly moves elements when should copy them - if has connect issue or so-question, don't refrain pointing it.
so question is: there specific reason behaviour in msvc (i.e. performance implications other copy vs. move or anything) or bug?
Comments
Post a Comment