13

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 C2338: The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.

If you read similar questions, the answer usually repeats the error message, but never goes into details or even gives an explanation as to why the static_assert was introduced. Defining containers of const elements doesn't seem to be an unreasonable thing to do, so why would C++11 forbid it? Since it used to be allowed in earlier versions, what is the exact reason for this limitation?

Here is sample code which fails to compile in VS2017:

#include "stdafx.h"
#include <vector>

struct foo {};

typedef std::vector<const foo> foo_vector;

int main(int argc, char** argv)
{
    foo_vector v;
    return 0;
}

Regarding the possibly duplicate question, it neither provides background nor a reliable answer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
chris
  • 319
  • 2
  • 12
  • 8
    Do you understand _"allocator is ill-formed"_? – Passer By Jul 28 '18 at 16:28
  • 4
    What would you like to achieve with such constructs? That sounds like an xy-problem. – πάντα ῥεῖ Jul 28 '18 at 16:31
  • 7
    Well, as I said, you just repeat the error message as an explanation. – chris Jul 28 '18 at 16:35
  • By the way: std::array is an exception from... – Aconcagua Jul 28 '18 at 16:47
  • 3
    How could you implement `insert()` in a `vector` without forcing a full reallocaiton of the vector regardless of capacity? You'd have a bunch of these hidden implementation inconsistencies between const and non-const versions. –  Jul 28 '18 at 16:47
  • 7
    [Does C++11 allow vector?](//stackoverflow.com/a/39652132) – Baum mit Augen Jul 28 '18 at 16:48
  • vector container is a dynamic array. So vector is never constant. Compiler doesn't like it. Don't use it. – Nguai al Jul 28 '18 at 16:54
  • 1
    @Frank: But that's what I want to find out - does it not make sense or did the STL implementors just screw up. – chris Jul 28 '18 at 17:14
  • Should the const elements be allocated in read-only space or read-write space? – stark Jul 28 '18 at 17:30
  • 1
    Can you give us a use case for this? Just to understand better in which circumstances it is needed. – geza Jul 28 '18 at 17:34
  • @Frank: why would a full reallocation needed for an insert? It's just a simple placement new. – geza Jul 28 '18 at 17:35
  • If it so much important to you that the vector will contain only const elements, you can simply implement it.. – Coral Kashri Jul 28 '18 at 17:40
  • @geza You can't replace something that's `const`, you can only create or destroy it, that's kind of the whole point. –  Jul 28 '18 at 18:33
  • @Frank: Yes, but you can move elements around in-place using only destruction and placement new, same as the vector does when resizing without reallocation (note I do mean "resize", not "insert"... insert mainly uses move assignments) – Ben Voigt Jul 28 '18 at 19:03
  • @Nguaial `std::vector a;` is not the same as `const std::vector a;` – JHBonarius Jul 28 '18 at 21:17
  • 1
    @BaummitAugen seems like, for c++17, they finally understood the problem – chris Nov 19 '18 at 09:47
  • 1
    This issue seems to be fixed in VS2019. – chris Oct 31 '19 at 07:15

0 Answers0