Linked Questions
39 questions linked to/from Does C++11 allow vector<const T>?
16 votes
2 answers
6k views
Why does stack<const string> not compile in g++? [duplicate]
I encountered this problem maintaining a port for a large (relative to the size of our team) project, but it was simple to create a small example. stackoverflow.cpp: #include<iostream> #include&...
9 votes
4 answers
19k views
vector of const pointers? [duplicate]
The following doesn't compile (very verbose error, but basically "cannot be overloaded" and "invalid conversion from 'const void*' to 'void*'"). I can understand why for example push_back() may not ...
11 votes
1 answer
7k views
Why can't I push_back to a vector of const elements? [duplicate]
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;...
13 votes
0 answers
4k views
Why does the C++ standard forbid containers of const elements? [duplicate]
What is the rationale behind C++11 forbidding containers of const elements? I am referring to the following error message, which you get if you define, for example, a vector of const elements: error ...
0 votes
0 answers
544 views
why std::vector cannot hold components has constant/volatile or reference objects? [duplicate]
As this answer said: The component type of containers like vectors must be assignable. References are not assignable (you can only initialize them once when they are declared, and you cannot make ...
0 votes
0 answers
245 views
Cannot push_back vector of const pointers [duplicate]
I want to make a vector of const pointers. (the pointers being constant, not A being constant) This code here gives me the error: class A{}; std::vector<A * const> vec; //creates vector of ...
4 votes
0 answers
113 views
Container of const objects? [duplicate]
First of all: I'm still a beginner in c ++, but I don't really understand the following: So someone said to me, that if i have for example a container of references (e.g. map), it is not working ...
3 votes
0 answers
127 views
What is the philosophy behind preventing a vector of consts [duplicate]
The standard library vector prevents us from constructing a vector of consts. My question is why? I read the answers on apparently similar questions to mine and I'm not really convinced. So let's dig ...
5 votes
0 answers
92 views
Why is it impossible to create an std::set<const std::string> in C++? [duplicate]
I have a class with std::set<const std::string> members which compiles fine in Clang 10.0.1 on macOS, but does not with GCC 5.4.0. MRE below #include <set> #include <string> class ...
0 votes
0 answers
39 views
the requirement of T for vector<T> used in default initialization [duplicate]
vector<const int>() fails to compile in C++14 (and in C++03). Under C++03, we can say const int is not CopyAssignable, so naturally it fails. However, under C++14, the wording in the standard ...
68 votes
4 answers
81k views
const vector implies const elements?
Does const vector<A> mean that its elements are constas well? In the code below, v[0].set (1234); in void g ( const vector<A> & v ) produces the compiler error const.cpp:28:3: ...
49 votes
2 answers
25k views
Vector of const objects giving compile error
I have declared the following in my code vector <const A> mylist; I get the following compile error - new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(...
8 votes
3 answers
6k views
Why can't you put a const object into a STL container?
See the code below - I am trying to put a const object into a vector. I know the answer is "STL containers require objects to be assignable and copy constructable", but, without citing the standard, ...
6 votes
3 answers
2k views
Correct behaviour for std::vector<T>::value_type
After scratching my head at some errors in some template code that used std::vector::value_type I tracked it down to the following. Is this correct behavior according to the standard, or is this a ...
8 votes
3 answers
6k views
boost::optional not letting me reassign const value types
It seems to me there should be four variants of boost::optional optional<Foo> => holds a mutable Foo and can be reassigned after initialization optional<Foo const> const => holds a const ...