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

冷暖自知 提交于 2020-07-02 18:08:33

问题


Or can it be a separate unsigned integer type?

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


回答1:


The C++ Standard says:

18.2/2 The contents are the same as the Standard C library header , with the following changes:

18.2/6 The type size_t is an implementation-defined unsigned integer type 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 conversion ranks (4.13) are no greater than that of signed long int unless a larger size is necessary to contain all the possible values. —end note ]

So, it doesn't say explicitly whether the implementation-defined unsigned integer type will be one of unsigned short, int, long, long long. The fact that 18.2/6 exists and specifies an "implementation-defined unsigned integer type" may be seen to override 18.2/2's default of following C, so any answer for C can't be trusted for C++.

The recommendation re conversion ranks implies the size_t will be expected to be one of the types mentioned in 4.13, where size_t isn't explicitly mentioned but the obvious candidates are, but that's no guarantee.

Do I need to provide a separate specialization for size_t?

You could use std::is_same and std::enable_if to do so when size_t is a distinct type....




回答2:


Text from [support.types]:

The contents are the same as the Standard C library header , with the following changes:

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

From the C99 specification of stddef.h there is also this footnote for clarification:

224) Some of these types may denote implementation-defined extended integer types.

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

Having said that, I don't know of any implementation for which it is not a typedef.

I'm not sure what you should do about your overload problem, however note that it is not limited just to size_t; there is also ptrdiff_t, and all of the fixed-width integer types. The latter are specified as being typedefs, however they are allowed to be aliases for extended integer types.




回答3:


From the standard (§ 18.2) from N3797 published on 13.10.2013:

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

So it must be an unsigned integral type, but the actual size is implementation defined.



来源:https://stackoverflow.com/questions/23749822/is-size-t-guaranteed-to-be-an-alias-type-to-one-of-integer-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!