On my system, wchar_t and int are distinct types with the same properties:
#include <type_traits>
sizeof(wchar_t) == sizeof(int) == 4
std::is_signed<wchar_t> == std::is_signed<int> == std::true_type
std::is_same<wchar_t, int> == std::false_type
In contrast, ptrdiff_t
and long int
are identical types (same properties, and is_same
is true).
Is this distinctness of wchar_t
guaranteed? Is it safe to overload for wchar_t
and int
on all systems? Is there any property in or elsewhere that distinguishes wchar_t
and the corresponding int property besides is_same
?
(System info: I'm interested in the general case, but my tests so far have been on an OS X machine running g++ 4.8.0 and Apple clang++ 4.1, both with -std=c++11.)
The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.
wchar_t is unsigned. Corresponding assembly code says movzwl _BOM, %eax .
int , long , ptr , and off_t are all 32 bits (4 bytes) in size. int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size. The 32-bit data model for z/OS® XL C/C++ compilers is ILP32 plus long long.
Yes, wchar_t
is guaranteed to be a distinct type (§3.9.1/5):
Type
wchar_t
is a distinct type whose values can represent distinct codes for all members of the largest extended character set specified among the supported locales (22.3.1).
So yes, it's safe to overload for wchar_t
and int
on all systems.
However, wchar_t
is also guaranteed to have the same size, signedness and alignment requirements as another integral type, which is its underlying type. This isn't necessarily int
but in your case appears to be. This means wchar_t
is probably implemented using one of the integral types, but as far as you are concerned, they are treated as completely distinct types.
Yes, for C++11, wchar_t is its own type, distinct from any other, but as you've observed, it will also have the same range, signedness, etc., as some other type (§3.9.1/3):
Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest extended character set specified among the supported locales (22.3.1). Type wchar_t shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With