简体   繁体   English

容器中的大小大于max_size

[英]size greater than max_size in containers

What standard says about situation when size of the container is greater than max_size? 当容器的大小大于max_size时,关于情况的标准是什么?

UB, std::bad_alloc or something else? UB,std :: bad_alloc还是其他什么?

MSVC throws an exception. MSVC抛出异常。

I'm assuming you mean 'what happens if I try to push the container over max_size ?' 我假设你的意思是'如果我试图将容器推到max_size会发生什么?' because a container's size cannot exceed max_size . 因为容器的size不能超过max_size If it does, then max_size returned an incorrect value. 如果是,则max_size返回不正确的值。

Exactly what happens depends on the container and what operation is attempting to resize the container, but in the case of most re-sizable containers (ie string , vector ), the standard requires a length_error to be thrown. 究竟发生了什么取决于容器以及尝试调整容器大小的操作,但在大多数可重复大小的容器(即stringvector )的情况下,标准需要抛出length_error

The standard requires a std::length_error to be thrown in most cases, but in some cases the allocator may throw a different exception. 在大多数情况下,标准要求抛出std::length_error ,但在某些情况下,分配器可能会抛出不同的异常。

From C++03 §21.3/4a (Class template basic_string ): 从C ++03§21.3/ 4a(类模板basic_string ):

For any string operation, if as a result of the operation, size() would exceed max_size() then the operation throws length_error . 对于任何字符串操作,如果作为操作的结果, size()将超过max_size()则操作将抛出length_error

§21.3.3/10-12 ( basic_string capacity): §21.3.3/ 10-12( basic_string capacity):

void reserve(size_type res_arg=0) ; void reserve(size_type res_arg=0) ;
[...] [...]
Throws: length_error if res_arg > max_size() . 抛出: length_error如果res_arg > max_size() 218) 218)

218) reserve() uses Allocator::allocate() which may throw an appropriate exception. 218) reserve()使用Allocator::allocate() ,它可能抛出适当的异常。

§23.2.4.2/2-4 ( vector capacity): §23.2.4.2/ 2-4( vector容量):

void reserve(size_type n)
[...] [...]
Throws: length_error if n > max_size() . 抛出: length_error如果n > max_size() 248 248

248) reserve() uses Allocator::allocate() which may throw an appropriate exception. 248) reserve()使用Allocator::allocate() ,它可能抛出适当的异常。

The standard doesn't explicitly mention this for the other standard containers ( deque , list , priority_queue , map , multimap , set , multiset , and bitset ). 该标准没有明确提及其他标准容器( dequelistpriority_queuemapmultimapsetmultisetbitset )。 However, in Table 65 (Container requirements), it says that max_size is the " size() of the largest possible container". 但是,在表65(容器要求)中,它表示max_size是“最大可能容器的size() ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM