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

emlai

push_backing to a vector of non-const elements works as expected:

std::vector<int> foo;
int bar = 0;
foo.push_back(bar);

But why is the following not possible?

std::vector<const int> foo;
const int bar = 0;
foo.push_back(bar);

More precisely, why is creating the foo object possible but not calling push_back on it?

M.M

According to this answer (with commentary from one of the C++11 designers), std::vector<const T> is not permitted by the Standard.

The answer suggests that it might be possible to supply a custom allocator which permits a vector with that allocator to hold const objects.

You're probably better off not attempting to do this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't push_back a class into an object vector inside a for loop

From Dev

Can't push_back an unique_ptr in a vector

From Dev

Can't push_back a class containing an ofstream pointer to a vector

From Dev

Why are there two overloads for vector::push_back?

From Dev

can i check content of values that have been push_back to a vector?

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

vector::push_back conversion from const Type* to type*

From Dev

PushBack (push_back()) elements of QStringList to vector<string>

From Dev

Why can't I change objects in a vector?

From Dev

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

From Dev

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

From Dev

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

From Dev

vector push_back doesn't work c++

From Dev

Why can't I draw elements to the stage?

From Dev

why I can't accessing this array elements

From Dev

Why can't I wrap a T* in an std::vector<T>?

From Dev

Why are non-const vector<bool> elements const?

From Dev

Why are non-const vector<bool> elements const?

From Dev

Since arrays decay to pointers, why can't I apply const?

From Dev

Why can't I use const when returning a reference?

From Dev

Why can't I use const when returning a reference?

From Dev

Why can't I use this function with inside a function with const?

From Dev

Why can't I mark this member function as const?

From Dev

Why can't I add items into my vector?

From Dev

Why can't I create a vector of threads on the fly like this

From Dev

Why can't I convert a vector into a matrix with 'as.matrix' function?

From Dev

Why can't I access directly in vector iterator?

From Dev

Why can't i construct a vector by passing temporary input iterator?

Related Related

  1. 1

    Can't push_back a class into an object vector inside a for loop

  2. 2

    Can't push_back an unique_ptr in a vector

  3. 3

    Can't push_back a class containing an ofstream pointer to a vector

  4. 4

    Why are there two overloads for vector::push_back?

  5. 5

    can i check content of values that have been push_back to a vector?

  6. 6

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

  7. 7

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

  8. 8

    vector::push_back conversion from const Type* to type*

  9. 9

    PushBack (push_back()) elements of QStringList to vector<string>

  10. 10

    Why can't I change objects in a vector?

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

    vector push_back doesn't work c++

  15. 15

    Why can't I draw elements to the stage?

  16. 16

    why I can't accessing this array elements

  17. 17

    Why can't I wrap a T* in an std::vector<T>?

  18. 18

    Why are non-const vector<bool> elements const?

  19. 19

    Why are non-const vector<bool> elements const?

  20. 20

    Since arrays decay to pointers, why can't I apply const?

  21. 21

    Why can't I use const when returning a reference?

  22. 22

    Why can't I use const when returning a reference?

  23. 23

    Why can't I use this function with inside a function with const?

  24. 24

    Why can't I mark this member function as const?

  25. 25

    Why can't I add items into my vector?

  26. 26

    Why can't I create a vector of threads on the fly like this

  27. 27

    Why can't I convert a vector into a matrix with 'as.matrix' function?

  28. 28

    Why can't I access directly in vector iterator?

  29. 29

    Why can't i construct a vector by passing temporary input iterator?

HotTag

Archive