I was trying to understand the following behaviour:
#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 the const
member of Foo
was implicitly deleting Foo
s copy-assignment operator, which std::vector::insert
used.
Why does std::vector::insert
need to copy assign while std::vector::push_back
copy constructs? Does this mean it can be more efficient to manually concatenate two vectors? This is using LLVM.