Home  >  Q&A  >  body text

c++ - 容器元素是const元素时的错误

"The C++ Standard forbids containers of const elements "
"because allocator<const T> is ill-formed."

这个ill-formed是什么意思?

PHP中文网PHP中文网2765 days ago786

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:14:23

    I don’t know what container you are using, if it is a vector, because when the vector adds elements, if there is not enough space, it will reallocate more memory, and copy the original vector elements and reassign them to the new vector. If the element type is const, reassignment is not supported. So the compiler will report an error

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:14:23

    Ill means pathological, bad and undesirable. Ill formed is the wrong form.
    How do you modify const T when it is new... The standard library container does not support const T because it cannot be modified. , you need to copy your elements like vector.

    reply
    0
  • Cancelreply