wchar_t aliasing

T

tni

What are the aliasing rules for wchar_t with regards to its underlying
int type? E.g., if wchar_t is 16-bits, is it safe to cast to uint16_t?
 
R

Robert Hairgrove

tni said:
What are the aliasing rules for wchar_t with regards to its underlying
int type? E.g., if wchar_t is 16-bits, is it safe to cast to uint16_t?

Section 3.9.1., paragraph 5 "Fundamental types" of the C++ standard
tells you what you need to know. Basically, it's size and signedness are
implementation and platform dependent.
 
J

James Kanze

What are the aliasing rules for wchar_t with regards to its
underlying int type?

That the underlying type is an integer type, whose values can
represent distinct codes for all members of the largest extended
character set specified among the supported locales.

In practice, constraints of backwards compatibility mean that
some widespread implementations aren't strictly conformant in
regard to that last criteria---formally, it means that if any
supported locale supports modern versions of Unicode, wchar_t
must have at least 21 bits.
E.g., if wchar_t is 16-bits, is it safe to cast to uint16_t?

The results of the cast will be well defined on all systems, but
there's no guarantee that information won't be lost. wchar_t is
often 32 bits (and would almost certainly be 32 bits almost
everywhere without considerations of backwards compatibility,
which constraint Windows and AIX to 16 bit values).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top