c++ - multicharacter literal misunderstanding -


i thought knew how msvc 2010 treated multicharacter literals, until this:

int main(int argc, char* argv[])  {     int = '\'   ';     int b = '\'/  ';     int c = '\'/> ';     int d = '\'/>\x20';  // same c supposedly     int e = 'abc\x20';     printf("%x\n%x\n%x\n%x  <-- what?\n%x\n", a,b,c,d,e);     return 0; }  27202020 272f2020 272f3e20 20272f3e  <-- what? 41424320 

in ide's watch window if type:

'\'/>\x20' 

it prints out:

272f3e20 

which expect. so... what's going on here!?

i found this on net, i'm thinking it's compiler bug. guess might not fixed because break older code?

edit: i'm pretty satisfied quirk or bug isn't going change. seems occur when there more 1 escape sequence being used in multicharacter literal.

here workaround:

('\'/>' << 8) | '\n' 

this appears known msvc compiler 'peculiarity'.

the c++ standard n3797 s2.14.3/1 says:

a multicharacter literal, or ordinary character literal containing single c-char not representable in execution character set, conditionally-supported, has type int, , has implementation-defined value.

so msvc can , claim 'implementation-defined' , not bug.

if call, 'do not fix'. risk of breaking existing code far higher benefit of doing useful, , dealt interesting question , answer on stack overflow.

ref: see http://www.tech-archive.net/archive/vc/microsoft.public.vc.language/2004-09/0079.html.


if wish reliably assemble equivalent values have 2 choices, produce opposite results depending on endianism.

you can use arithmetic operations (shift , mask) produce integer value:

 '\'' | ('/' << 8) | ('>' << 16) | ('\x20' << 24) 

or can use string , cast operations produce string-like integer value:

*(int*)"\"/>\x20" 

as per comment, depending on how written last technique can lead generation of bad code. string has go somewhere (at run-time) , null-terminated. main justification can avoid need endian-sensitive #defines , pre-processing.

see question: how write compile-time initialisation of 4 byte character constant portable


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -