C ++ 11允许向量< const T>? [英] Does C++11 allow vector<const T>?

查看:143
本文介绍了C ++ 11允许向量< const T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

容器需求已从C ++ 03更改为C ++ 11。虽然C ++ 03有一些要求(例如拷贝构造性和向量的可分配性),但C ++ 11定义了每个容器操作的细粒度要求(第23.2节)。

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).

结果,你可以eg在向量中存储一个可复制构造但不可分配的类型(例如具有const成员的结构),只要执行某些不需要赋值的操作(构造和 push_back 是这样的操作; insert 不是)。

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).

我想知道的是:意味着标准现在允许向量< const T> ?我没有看到任何理由,它不应该 - const T ,就像一个结构与一个const成员,是一个类型是复制构造,但不可分配 - 但我可能错过了一些东西。

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.

(部分原因是让我觉得我可能错过了一些东西,是gcc中断崩溃和烧伤,如果你尝试实例化 vector< const T> ,但是对于向量< T> 很好,其中T有一个const成员)。

(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).

推荐答案

不,我相信分配器要求说T可以是非常量,非引用对象类型。

No, I believe the allocator requirements say that T can be a "non-const, non-reference object type".

你不能用常量对象的向量做很多事情。并且 const向量< T> 几乎是相同的。

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.

这篇关于C ++ 11允许向量&lt; const T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆