Why std::vector::push_back needs the assignment operator

ToBe
std::vector::push_back(constT& value)

requires the type T to be CopyInsertable according to this .

However, compiling the following program with failes (clang, GCC, Visual; both without c++11) unless I provide a public assignment operator.

#include <vector>

class A {
  A& operator= (const A& rhs); //private !! 
};

int main()  {
 std::vector<A> v;
 A a;
 v.push_back(a);
}

Why do I need to provide this assignment operator, I was under the impression that the copy construct was enough.

P.S. I could not find the place in the standard where this is defined, so if you could point to the reference, I would be most grateful

juanchopanza

The reference you quote applies to C++11. However, the C++03 standard has stricter requirements on types that can be stored in containers:

23.1 Container requirements [lib.container.requirements]

...

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.

(emphasis mine.) These requirements have been greatly relaxed in C++11, and are usually expressed in terms of the specific operations performed on containers. In that standard, your code would be valid, since the only requirement would be that A be CopyInsertable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Taking the argument by && in std::vector push_back() and std::map operator[]

From Dev

std::vector::erase(item) needs assignment operator to be defined for item?

From Dev

std::vector::erase(item) needs assignment operator to be defined for item?

From Dev

Why push_back is slower than operator[] for a previously allocated vector

From Dev

Why std::vector::push_back segfaults with virtual destructor?

From Java

Insert or push_back to end of a std::vector?

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

vector::push_back and std::move

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

Why are there two overloads for vector::push_back?

From Dev

push_back versus operator[] assignment in c++ vectors

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

no matching function for call to std::vector<std::tuple> push_back

From Dev

Thread safety std::vector push_back and reserve

From Dev

Cost of std::vector::push_back either succeeding or having no effect?

From Dev

Weird segment fault in function push_back of std::vector

From Dev

std::vector segmentation fault during push_back

From Dev

Thread safety std::vector push_back and reserve

From Dev

std::vector of OpenCV points, no push_back method

From Dev

Examples where std::vector::emplace_back is slower than std::vector::push_back?

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Why using new operator instead of std::vector?

From Dev

std::vector<std::vector<int>> push_back gives heap-buffer-overflow

From Dev

Is a std::unique_ptr moved into a std::vector when using push_back?

From Dev

std::string in object being deleted during push_back to std::vector

From Dev

Why std::move is required to invoke move assign operator of std::vector

From Dev

[ performance]--- string::operator+= vs. vector<char> push_back

From Dev

vector push_back in STL?

Related Related

  1. 1

    Taking the argument by && in std::vector push_back() and std::map operator[]

  2. 2

    std::vector::erase(item) needs assignment operator to be defined for item?

  3. 3

    std::vector::erase(item) needs assignment operator to be defined for item?

  4. 4

    Why push_back is slower than operator[] for a previously allocated vector

  5. 5

    Why std::vector::push_back segfaults with virtual destructor?

  6. 6

    Insert or push_back to end of a std::vector?

  7. 7

    Implementatnion of std::vector::push_back in MSVC

  8. 8

    vector::push_back and std::move

  9. 9

    Implementatnion of std::vector::push_back in MSVC

  10. 10

    Why are there two overloads for vector::push_back?

  11. 11

    push_back versus operator[] assignment in c++ vectors

  12. 12

    Calling std::vector::push_back() is changing previous elements in vector?

  13. 13

    Calling std::vector::push_back() is changing previous elements in vector?

  14. 14

    no matching function for call to std::vector<std::tuple> push_back

  15. 15

    Thread safety std::vector push_back and reserve

  16. 16

    Cost of std::vector::push_back either succeeding or having no effect?

  17. 17

    Weird segment fault in function push_back of std::vector

  18. 18

    std::vector segmentation fault during push_back

  19. 19

    Thread safety std::vector push_back and reserve

  20. 20

    std::vector of OpenCV points, no push_back method

  21. 21

    Examples where std::vector::emplace_back is slower than std::vector::push_back?

  22. 22

    Why can't I push_back to a vector of const elements?

  23. 23

    Why using new operator instead of std::vector?

  24. 24

    std::vector<std::vector<int>> push_back gives heap-buffer-overflow

  25. 25

    Is a std::unique_ptr moved into a std::vector when using push_back?

  26. 26

    std::string in object being deleted during push_back to std::vector

  27. 27

    Why std::move is required to invoke move assign operator of std::vector

  28. 28

    [ performance]--- string::operator+= vs. vector<char> push_back

  29. 29

    vector push_back in STL?

HotTag

Archive