Unicode in code oddity

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Reaction score
405
Location
image.png

The above C++ code looks wierd, but it's valid?, and compiles fine because of a unicode oddity, zero width space.

The above code works because it is written like this.
Code:
#include <iostream>

int main()
{
    int abc = 1;
    int ab\u200Bc = 2;
    int a\u200Bbc = 3;

    std::cout << abc << std::endl; // prints 1
    std::cout << ab\u200Bc << std::endl; // prints 2
    std::cout << a\u200Bbc << std::endl; // prints 3

    return 0;
}
 
Unicode recommends normalisation before compilation (UAX31) so this shouldn't be possible if your linter / compiler comply.

Doubt many do.

Xcode, Swift and by extension LLVM, certainly don't. e.g. compiles fine on Swift:

Screen Shot 2016-06-19 at 6.17.44 AM.png
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X