Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is wchar_t guaranteed to be distinct from any integer type?

Tags:

c++

c++11

wchar-t

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.)

like image 257
addaon Avatar asked Dec 04 '12 19:12

addaon


People also ask

What is the size of wchar_t in C ++?

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.

Is wchar_t signed?

wchar_t is unsigned. Corresponding assembly code says movzwl _BOM, %eax .

How many bits is an int C++?

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.


2 Answers

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.

like image 81
Joseph Mansfield Avatar answered Nov 15 '22 06:11

Joseph Mansfield


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.

like image 32
Jerry Coffin Avatar answered Nov 15 '22 04:11

Jerry Coffin