[英]Is std::container::size_type guaranteed to be size_t for standard containers with default allocator?
Like: 喜欢:
std::string<T>::size_type
std::list<T>::size_type
std::map<T>::size_type
std::vector<T>::size_type
Both cplusplus.com and cppreference.com say that they are usually size_t
, but are they truly, unambiguously guaranteed by the standard to be size_t
unless a custom allocator is used? cplusplus.com和cppreference.com都说它们通常是size_t
,但除非使用自定义分配器,否则它们是否真的,毫无疑问地被标准保证为size_t
?
For STL-containers - nope. 对于STL容器 - 不。 Table 96 of the standard in [container.requirements.general], which lists container requirements for any container X
, explains it pretty clear: [container.requirements.general]中标准的表96列出了任何容器X
容器需求,非常清楚地说明了这一点:
However, for basic_string
, size_type
is defined as 但是,对于basic_string
, size_type
定义为
typedef typename allocator_traits<Allocator>::size_type size_type;
which in turn will be size_t
for std::allocator<..>
as the allocator. 反过来, std::allocator<..>
size_t
将作为分配器。
Also, std::array
uses size_t
as size_type
, according to [array.overview]/3. 此外,根据[array.overview] / 3, std::array
使用size_t
作为size_type
。
size_type
isn't guaranteed to be size_t
. size_type
不保证是size_t
。
But the default allocator size_type
is, so the default is size_t
. 但是默认的分配器 size_type
是,所以默认值是size_t
。
From the standard 20.6.9 从标准20.6.9
template <class T> class allocator {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
....
The container's size_type is derived from the allocator: 容器的size_type派生自分配器:
typedef typename allocator_traits<Allocator>::size_type size_type;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.