Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top