Does the aliasing loophole apply to signed characters?ithix_ Jj1hoHSsc Dnd hKo Pŝto Ssy.de
In C++ there is an aliasing loophole which allows the object representation of any object to be read or written through some pointers of character type.
Does this apply only to char and unsigned char or also to signed char?
1 Answer
No, the provision does not extend to signed char.
[basic.lval]
8 If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
- [...]
- a
char,unsigned char, orstd::bytetype.
The quote above contains the very last bullet that pertains to aliasing with character types. signed char is excluded.
Nevertheless, this is also part of the subject CWG Issue 350 deals with, and so may change. Given the direction the issue has taken, the intent is for it to be (eventually, hopefully?) well-defined.
-
That's quite interesting. Why is
charallowed, which is signed on my machine, but notsigned char? But that's probably the reason why they want to add it in an upcoming standard. – mch 9 hours ago -
@mch - Could be an editorial mistake. I can only guess, really. – StoryTeller 9 hours ago
-
3@mch It's because in some cases, the standard does not allow
charto be signed. For example, trap representations are allowed in all signed types, so asigned charmay not be able to represent every byte, andcharwould have to be unsigned. – Artyer 8 hours ago -
2@Artyer - Can't believe I forgot that. Though it's worth noting that with C++20 moving towards "2's complement only",
signed charno longer has place for trap values eel.is/c++draft/basic.fundamental#7. – StoryTeller 7 hours ago -
I guess the
charis left from C compatibility? Just like the entirechartype :D – Antti Haapala 12 mins ago