Is std :: container :: size_type a guaranteed size_t for standard containers with a default dispenser?

how

  • std::string<T>::size_type
  • std::list<T>::size_type
  • std::map<T>::size_type
  • std::vector<T>::size_type
  • and etc.

As cplusplus.com and cppreference.com say that they are usually size_t , but are they uniquely guaranteed by the standard like size_t if a custom allocator is not used?

+5
source share
2 answers

For STL containers, no. The standard table 96 in [container.requirements.general], which lists the container requirements for any container X , explains this quite clearly:

enter image description here


However, for basic_string , size_type is defined as

 typedef typename allocator_traits<Allocator>::size_type size_type; 

which in turn will be size_t for std::allocator<..> as a distributor.

In addition, std::array uses size_t as size_type , according to [array.overview] / 3.

+7
source

size_type not guaranteed by size_t .

But the default size_type , so the default is size_t .

From standard 20.6.9

 template <class T> class allocator { public: typedef size_t size_type; typedef ptrdiff_t difference_type; .... 

The size_ type of the container is inferred from the dispenser:

 typedef typename allocator_traits<Allocator>::size_type size_type; 
0
source

Source: https://habr.com/ru/post/1204948/


All Articles