问题描述
容器要求已从 C++03 更改为 C++11.虽然 C++03 有全面的要求(例如向量的复制构造性和可分配性),但 C++11 定义了对每个容器操作的细粒度要求(第 23.2 节).
因此,您可以例如将可复制构造但不可赋值的类型(例如具有 const 成员的结构)存储在向量中,只要您仅执行某些不需要赋值的操作(构造和 push_back
就是这样操作;insert
不是).
我想知道的是:这是否意味着标准现在允许 vector
?我看不出有任何不应该这样做的理由 - const T
,就像具有 const 成员的结构一样,是一种可复制构造但不可分配的类型 - 但我可能遗漏了一些东西.
(部分原因让我觉得我可能错过了什么,如果您尝试实例化 vector
,gcc 主干会崩溃并烧毁,但是 vector
其中 T 有一个 const 成员).
不,我相信分配器要求说 T 可以是非常量、非引用的对象类型".
对于常量对象的向量,您将无法做太多事情.无论如何,const vector
几乎是一样的.
多年后,这个快速而肮脏的答案似乎仍然吸引着评论和投票.不总是向上.:-)
所以添加一些适当的引用:
对于我写在纸上的 C++03 标准,[lib.allocator.requirements] 部分中的表 31 说:
<块引用>T, U 任意类型
不是那种任何类型的实际工作.
因此,下一个标准 C++11 在 [allocator.requirements] 中说在一个关闭草案中现在是表 27:
<块引用>T、U、C 任何非常量、非引用的对象类型
这与我最初根据记忆在上面写的内容非常接近.这也是问题的内容.
但是,在 C++14(草案 N4296)中,表 27 现在说:
<块引用>T、U、C 任何非常量对象类型
可能是因为引用毕竟不是对象类型?
现在在 C++17(草案 N4659)中,表 30 表示:
<块引用>T、U、C 任何 cv 非限定对象类型 (6.9)
所以不仅排除了const
,还有volatile
.反正可能是旧新闻,只是澄清一下.
另请参阅Howard Hinnant 的第一手信息,目前就在下方.>
Container requirements have changed from C++03 to C++11. While C++03 had blanket requirements (e.g. copy constructibility and assignability for vector), C++11 defines fine-grained requirements on each container operation (section 23.2).
As a result, you can e.g. store a type that is copy-constructible but not assignable - such as a structure with a const member - in a vector as long as you only perform certain operations that do not require assignment (construction and push_back
are such operations; insert
is not).
What I'm wondering is: does this mean the standard now allows vector<const T>
? I don't see any reason it shouldn't - const T
, just like a structure with a const member, is a type that is copy constructible but not assignable - but I may have missed something.
(Part of what makes me think I may have missed something, is that gcc trunk crashes and burns if you try to instantiate vector<const T>
, but it's fine with vector<T>
where T has a const member).
No, I believe the allocator requirements say that T can be a "non-const, non-reference object type".
You wouldn't be able to do much with a vector of constant objects. And a const vector<T>
would be almost the same anyway.
Many years later this quick-and-dirty answer still seems to be attracting comments and votes. Not always up. :-)
So to add some proper references:
For the C++03 standard, which I have on paper, Table 31 in section [lib.allocator.requirements] says:
T, U any type
Not that any type actually worked.
So, the next standard, C++11, says in a close draft in [allocator.requirements] and now Table 27:
T, U, C any non-const, non-reference object type
which is extremely close to what I originally wrote above from memory. This is also what the question was about.
However, in C++14 (draft N4296) Table 27 now says:
T, U, C any non-const object type
Possibly because a reference perhaps isn't an object type after all?
And now in C++17 (draft N4659) it is Table 30 that says:
T, U, C any cv-unqualified object type (6.9)
So not only is const
ruled out, but also volatile
. Probably old news anyway, and just a clarification.
Please also see Howard Hinnant's first-hand info, currently right below.
这篇关于C++11 是否允许向量<const T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!