I tried to understand the following behavior:
#include <vector> #include <iterator> struct Foo { Foo(int a) : a_ {a} {} const int a_; // Note the const }; int main(int argc, char **argv) { std::vector<Foo> v1 {Foo {0}}; std::vector<Foo> v2 {Foo {1}}; auto first = std::begin(v2); auto last = std::end(v2); for (; first != last; ++first) { v1.push_back(*first); // Fine } //v1.insert(v1.begin(), first, last); // Does not compile return 0; }
It turns out that the const Foo member implicitly deletes the Foo copy-assign statement, which is used by std::vector::insert .
Why does std::vector::insert need to copy the destination when std::vector::push_back copies? Does this mean that it can be more efficient to manually concatenate two vectors? It uses LLVM.
c ++ vector c ++ 11
Daniel
source share