Is size_t guaranteed to be an alias for one of the integer types?

Or could it be a single unsigned integer type?

I have different specializations of the template function for different (unsigned) integer types. Do I need to provide a separate specialization for size_t ?

+6
source share
3 answers

The C ++ standard says:

18.2 / 2 Content is the same as the title of the Standard C library with the following changes:

18.2 / 6 The size_t type is an unsigned integer type defined for implementation that is large enough to contain the size in bytes of any object.

18.2 / 7 [Note: it is recommended that implementations choose types for ptrdiff_t and size_t, whose integer number of conversion ranks (4.13) is not greater than the signed long int, if a larger size does not require all possible values. -end note]

Thus, it does not explicitly indicate whether the unsigned short integer type determined by the implementation will be one of unsigned short , int , long , long long . The fact that there is 18.2 / 6 and indicates the "unsigned integer type defined by the implementation" can be considered as overriding the default 18.2 / 2 value for C, so any answer for C cannot be trusted for C ++.

Ranking a re-evaluation of recommendations means that size_t will be considered one of the types mentioned in 4.13, where size_t is not explicitly mentioned, but are obvious candidates, but this does not guarantee.

Do I need to provide a separate specialization for size_t?

You can use std::is_same and std::enable_if for this when size_t is a separate type ....

+6
source

Text from [support.types] :

The content is the same as the title of the Standard C library with the following changes:

The size_t type is an unsigned integer type defined for implementation that is large enough to contain the size in bytes of any object.

The C99 specification for stddef.h also has this footnote for clarification:

224) Some of these types may indicate extended integer types defined by an implementation.

Since the standard C ++ text does not specifically say that size_t should be a typedef, and since it seems to be based on C99, it seems to me that we should conclude that it can be an extended integer type defined by the implementation.

Having said that, I do not know any implementation for which it is not a typedef.

I'm not sure what you should do with your overload problem, however note that this is not limited to size_t ; there is also ptrdiff_t and all fixed-width integers. The latter are listed as typedef s, however they are allowed to be aliases for extended integer types.

+1
source

From the standard (ยง 18.2) of N3797 published on 10/13/2013:

The size_t type is an unsigned integer type defined for implementation that is large enough to contain the size in bytes of any object.

Thus, it must be an unsigned integral type, but the actual size is determined by the implementation.

0
source

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


All Articles