C ++ 11是否更改了对STL容器元素的要求,以及如何更改? [英] Has C++11 changed requirements for elements of STL containers, and how?
问题描述
最近,我对 std :: unique_ptr
对于STL容器的元素是可接受的,因为我认为这些元素必须在下面提供功能(此页面表示相同):
Recently I'm surprised at the fact std::unique_ptr
is acceptable for elements of STL containers, because I thought these elements are required to provide functions below(this page says the same):
- 不带参数的公共默认构造函数
- 公共副本构造函数
- 公共副本赋值运算符函数
- 公共析构函数
- public default constructor with no arguments
- public copy constructor
- public copy assignment operator function
- public destructor
但是 std :: unique_ptr
不能复制以使其持有指针
But std::unique_ptr
is not copyable to make the pointer it holds owned by a single object, that contradicts the requirements above.
标准是否更改了要求?如果是这样,会有哪些变化?也许可移动对象或可复制对象已足够?
我已经在网上搜索了自C ++ 11以来要求是否发生了变化,但是找不到任何可以帮助我的页面...
Has the standard changed the requirements? If so, what are the changes? Perhaps either movable objects or copyable ones are sufficient? I've searched the web whether the requirements has changed since C++11, but I can't find any page which helps me...
推荐答案
是的,对标准库容器的要求已进行了重大更改。很难提供全面的列表(有很多 ),但是这里有一些重要的列表:
Yes, there have been major changes to the requirements for standard library containers. It's hard to provide a comprehensive list (there were a lot), but here are some important ones:
std :: vector
通常只要求其成员为 MoveConstructible 和 MoveAssignable 。 std :: vector有许多成员函数,它们具有更严格的要求。 vector :: push_back
需要Move 或 CopyConstructible (取决于您传递的是右值还是左值),但是新的 vector :: emplace_back
仅要求有一个使用给定值的可访问构造函数。参数(除了基线要求)。显然,任何试图调用 vector
的副本构造函数/赋值的尝试都要求该类型为CopyConstructible(即:您不能复制 std:
std::vector
generally requires only that its members be MoveConstructible and MoveAssignable. There are many member functions of std::vector that impose more strict requirements. vector::push_back
requires Move or CopyConstructible (depending on whether you pass an rvalue or lvalue), but the new vector::emplace_back
only requires that there is an accessible constructor that takes the given parameters (in addition to the baseline requirement). Obviously any attempt to invoke the vector
's copy constructor/assignment will require that the type be CopyConstructible (ie: you can't copy a std::vector<unique_ptr>
).
类似地,大多数其他容器都减少了对类型的限制。它们还具有 emplace
成员函数,使您可以就地构造成员,以及l / rvalue插入函数。这意味着您不必复制值;您可以移动它们或就地构建它们。
Similarly, most other containers have reduced the restrictions on the type. They also have emplace
member functions that allow you to construct the members in-place, as well as l/rvalue insertion functions. Which means that you don't have to copy values in; you can move them in or construct them in-place.
不需要任何构造函数或析构函数都是公开的;所有构造都是通过调用 allocator_traits< Allocator> :: construct
调用进行的。因此,如果提供分配器,则可以将构造函数/析构函数设为私有。好吧,当然,只要您的分配器类可以访问它们。
None of the constructors or destructors are required to be public; all construction takes place via calls to allocator_traits<Allocator>::construct
calls. So if you provide an allocator, you could make your constructors/destructors private. Well, as long as your allocator class can access them, of course.
总之,要求不是很严格,但要复杂一些。如果限制自己对容器执行某些操作,则可以避免很多事情。
In short, the requirements are much less strict, but they're a bit more complex. You can get away with a lot of things if you restrict yourself from performing certain operations on the container.
这篇关于C ++ 11是否更改了对STL容器元素的要求,以及如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!