对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t?

lam*_*fun 7 c++ portability stl

喜欢:

  • std::string<T>::size_type
  • std::list<T>::size_type
  • std::map<T>::size_type
  • std::vector<T>::size_type
  • 等等

无论cplusplus.comcppreference.com说,他们通常size_t,但他们真正的,明确保障的标准是size_t,除非使用自定义分配器?

Col*_*mbo 7

对于STL容器 - 不.[container.requirements.general]中标准的表96列出了任何容器的容器要求X,非常清楚地说明了这一点:

在此输入图像描述


但是,对于basic_string,size_type定义为

typedef typename allocator_traits<Allocator>::size_type size_type;
Run Code Online (Sandbox Code Playgroud)

这反过来将size_t用于std::allocator<..>为分配器.

另外,根据[array.overview]/3 std::array使用size_tas size_type.