std :: vector适用于不是默认可构造的类? [英] std::vector works with classes that are not default constructible?
问题描述
我在几个地方读过std :: vector要求它的模板参数是默认可构造的。今天我只是尝试了它的一个类,有一个 delete
d默认构造函数,我感到惊讶似乎是工作很好(与std :: vector的默认构造函数)。是这个可移植的行为,还是这是gcc的STL的实现细节,我应该假设向量要求它的模板参数是默认可构造的
I've read in several places that std::vector requires it's template argument to be default constructible. Today I just tried it with one of my classes that has a delete
d default constructor, and to my surprise it seems to be working just fine (with std::vector's default constructor). Is this portable behavior, or is this an implementation detail of gcc's STL and should I assume vector to require it's template argument to be default constructible?
推荐答案
C ++ 03中的要求是存储在容器中的类型是 CopyConstructible
和可分配
见§23.1集装箱要求)。但是,在C ++ 11中,这些要求是放宽的,并且倾向于适用于对容器执行的操作。因此,一个简单的默认构造没有要求(参见C ++ 11标准中的teble 96,§23.1)。
The requirement in C++03 is that types being stored in a container be CopyConstructible
and Assignable
(see §23.1 Container Requirements). However, in C++11 these requirements are relaxed, and tend to apply to the operations performed on the container. So a simple default construction has no requirements (see teble 96, §23.1 in C++11 standard).
一旦你试图复制一个向量,元素,您将满足 CopyInsertable
, CopyAssignable
, EmplaceConstructible
, MoveInsertable
, MoveAssignable
等要求
As soon as you try to copy a vector, or insert elements into it, you will meet the CopyInsertable
, CopyAssignable
, EmplaceConstructible
, MoveInsertable
, MoveAssignable
etc. requirements
这篇关于std :: vector适用于不是默认可构造的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!