Which one is correct?

M

Maximus

wchar_t ch2 = L'\u27BA';
wchar_t ch = '\u27BA';

On Visual C++ 2003 SP1, no compiler warnings are given at all. However, ch
!= ch2.

Which one is correct?
 
V

Victor Bazarov

[Answering only in comp.lang.c++, where I read it]

Maximus said:
wchar_t ch2 = L'\u27BA';
wchar_t ch = '\u27BA';

On Visual C++ 2003 SP1, no compiler warnings are given at all.
However, ch != ch2.

Which one is correct?

The effects of placing a universal-character-name in a narrow
character literal (without the L in front) are implementation-
defined, you better ask in 'microsoft.public.vc.language'. As to
warnings, none required since this is not ill-formed.

I would venture a guess that you need L there to be "correct",
since that makes the character literal to have the type 'wchar_t',
which is what you declare 'ch2'. Without the L, the type of the
character literal is 'char', and how it gets converted into the
'wchar_t' for 'ch' is implementation-defined.

V
 
V

Victor Bazarov

Mathias said:
27BA is 10170.
'\u27BA' being of type char, it can't be greater than 255, assuming 8-
bit bytes. So it can't contain 10170.

<nitpick>
Actually, it's implementation-defined. The standard only guarantees
that the upper value is no less than 127, however (IOW, the char is
at least 8-bits). On a PC 'chars' are 8-bit and ususally signed, so
they cannot contain any values above 127...

V
 
M

Mathias Gaunard

wchar_t ch2 = L'\u27BA';
wchar_t ch = '\u27BA';

On Visual C++ 2003 SP1, no compiler warnings are given at all. However, ch
!= ch2.

Which one is correct?

27BA is 10170.
'\u27BA' being of type char, it can't be greater than 255, assuming 8-
bit bytes. So it can't contain 10170.
L'\u27BA' being of type whar_t, which is on all implementations I know
at least 16 bits, it can contain 10170.

Therefore the correct one is the first one.
 
B

BobR

Victor Bazarov wrote in message...
<nitpick>
Actually, it's implementation-defined. The standard only guarantees
that the upper value is no less than 127, however (IOW, the char is
at least 8-bits). On a PC 'chars' are 8-bit and ususally signed, so
they cannot contain any values above 127...
</nitpick>

But (on win, P4):
std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2
 
T

Thomas J. Gritzan

BobR said:
Victor Bazarov wrote in message...

But (on win, P4):
std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2

He talks about char, you talk about wchar_t. Makes no sense.

So I try it, too:
On linux, unknown machine:

std::cout<<" sizeof(double) = "<<sizeof(double)<<std::endl;
// out: sizeof(double) = 8
 
J

James Kanze

Victor Bazarov wrote in message...
But (on win, P4):
std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2

But the type of '\u27BA' is char. So the resulting value must
be in the range CHAR_MIN...CHAR_MAX; by default, with VC++,
-128...127 (but if the /J option is given, 0...255).

The reason he's seeing different values is precisely because the
type of '\u27BA' is char, but the type of L'\u27BA' is wchar_t,
in which the value fits.
 
R

Ron Natalie

James said:
The reason he's seeing different values is precisely because the
type of '\u27BA' is char, but the type of L'\u27BA' is wchar_t,
in which the value fits.
Nothing precludes \u27BA from fitting in a char. While it is
true of Visual Studio he is using, it's not required by the
language. The 27BA is just a particular ISO character nothing
precludes it from being a valid value in the execution character
set (just not allowed to be part of the basic execution character
set).
 
R

Ron Natalie

Mathias said:
27BA is 10170.
'\u27BA' being of type char, it can't be greater than 255, assuming 8-
bit bytes. So it can't contain 10170.

Nothing says that \uXXXX maps directly into a character value. The
number after the u is an ISO 10646 (practically unicode) value. Do
not assume the execution character set is ASCII.
 
B

BobR

Thomas J. Gritzan said:
He talks about char, you talk about wchar_t. Makes no sense.

Sorry. Somehow I missed the '\u' in there.
(All I seemed to see was '27BA is 10170'.)

Like: Who put that '\u' in there *after* I read the line? <G>
 

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
474,292
Messages
2,571,498
Members
48,185
Latest member
abhaysingh01

Latest Threads

Top