Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I get the following compile error -

new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded

But if declare -

vector <A> mylist;

my code compiles.

Is const not allowed in this context?

I am copying my code here for everyone'e reference -

#include <iostream>
#include <vector>
using namespace std;
class A
public:
    A () {cout << "default constructor\n";}
    A (int i): m(i) {cout << "non-default constructor\n";}
private:
    int m;
int main (void)
    vector<const A> mylist;
    mylist.push_back(1);
    return 0;

Items in a vector must be assignable (or, in more recent versions of the standard, movable). const objects aren't assignable, so attempting to store them in a vector will fail (or at least can fail -- the code is invalid, but a compiler is free to accept it anyway, if it so chooses, though most programmers would generally prefer that invalid code be rejected).

I suppose for the truly pedantic, if you wanted to badly enough, you could define a type that was assignable despite being const, something like this:

class ugly { 
    mutable int x;
public:
    ugly const &operator=(ugly const &u) const { 
        x = u.x;
        return *this;

I believe you should be able to store items of this type in a vector even though they're const. A quick test of creating a vector of these succeeds with VC++. This failed with some older compilers (e.g., failed with g++ 4.8.1), but works with reasonably recent ones (VC++ back to at least 2015, g++ back to at least 5.4 and clang++ back to at least 4.0--though I haven't tried to track down the first version of each that supported it).

For a current compiler, a type that supported moving const objects would probably work just as well. But, just in case it wasn't obvious: this is allowing you to modify an object even though it's marked const. That's clearly a direct violation of any reasonable user's expectations, so it's mostly a problem, not a solution.

Actually you can use a vector over non-assignable items. Just you can not use a lot of member functions / algorithms, like push_back. – lip Jun 26, 2013 at 11:11 @lip: You might be able to get away with it in some cases, but I believe in every case the requirement in the standard is that T be either MoveAssignable or CopyAssignable. – Jerry Coffin Jun 26, 2013 at 13:11 We just ran into the same problem. The code compiled and works fine in VC++ (2012) but neither g++ 4.8.2 nor clang 3.4 like it. – ChrisWue Apr 3, 2014 at 21:19 The "items in a vector must be assignable" is from C++03; this requirement was relaxed for C++11. (However it is not completely clear whether a vector of const objects is permitted on C++11; see here) – M.M Mar 1, 2015 at 6:40 @AlwaysLearning Were you asking why someone would want a vector of const elements? I don't think it's a particularly outlandish scenario. It might be nice in some situations to call a function and say 'give me a bunch of Ts, but in a way that ensures I can't modify them later, although I reserve the right to add more to the container myself or to destroy them when I'm done'. That is what a non-const vector with a const value_type would provide, but due to the allocator requirements, we can't have that. cf also stackoverflow.com/questions/6954906/… – underscore_d Sep 23, 2018 at 11:13

The use of the push_back method is the problem. emplace_back will compile. Another alternative (depending on the whole situation you do not describe here) would be to use a vector<A const&> if the inserted items have a life outside the vector. Items in a vector do not need to be assignable, but when they are not, some member functions and algorithms can not be used.

Explanation :

push_back is supposed to first default-construct an A in the vector, then assign (using copy-construct) the given reference. This breaks your const qualification, hence does not compile.

emplace_back uses "perfect forwarding" to invoke the actual constructor directly in place.

Generally correct, but emplace_back does not use the object's move constructor. It uses "perfect forwarding" to invoke the actual constructor directly in-place. See (e.g.) en.cppreference.com/w/cpp/container/vector/emplace_back – Nemo Oct 5, 2014 at 0:36 I thought containers are not allowed to store references either (or at least not a vector. vector<A const&> does not compile – Mike Lui Dec 1, 2015 at 3:04 @underscore_d: Yeah--mostly I found it interesting (bordering on amazing) that years later, the visible score hadn't changed at all. – Jerry Coffin Nov 10, 2022 at 19:30

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.