Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++17 allow a non-ascii character as an identifier?

At cppref, I find a strange C++ code, which uses a non-ascii character in source code as follows:

template <char...> double operator "" _π(); // OK

However, the code above cannot be cmompiled with clang 6.0. The error message is:

error : source file is not valid UTF-8
1>double operator "" _<A6><D0>()
1>                    ^

My questions are:

  1. Is this conforming to C++17?
  2. Note that user-defined has no argument, then, how to use ? Just use double var = _π;?
like image 263
szxwpmj Avatar asked Apr 24 '18 13:04

szxwpmj


1 Answers

From the link you provided, it quotes:

If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a non-type template parameter pack with element type char

template <char...> double operator "" _x();

Let us see what this means,

Notation char... indicates that this template can be instantiated with 0, 1, 2 or more parameters of type char. This means that each time the compiler encounters a literal like 1234_km it should treat it as the following function call:

operator"" _km<'1', '2', '3', '4'>();

The entire string representing the literal is passed (chopped) as template argument. See this and this for usage.

And regarding the range of characters allowed:(See this Annexure E)

00A8, 00AA, 00AD, 00AF, 00B2-00B5, 00B7-00BA, 00BC-00BE, 00C0-00D6, 00D8-00F6, 00F8-00FF
0100-167F, 1681-180D, 180F-1FFF
200B-200D, 202A-202E, 203F-2040, 2054, 2060-206F
2070-218F, 2460-24FF, 2776-2793, 2C00-2DFF, 2E80-2FFF
3004-3007, 3021-302F, 3031-303F
3040-D7FF
F900-FD3D, FD40-FDCF, FDF0-FE44, FE47-FFFD
10000-1FFFD, 20000-2FFFD, 30000-3FFFD, 40000-4FFFD, 50000-5FFFD,
60000-6FFFD, 70000-7FFFD, 80000-8FFFD, 90000-9FFFD, A0000-AFFFD,
B0000-BFFFD, C0000-CFFFD, D0000-DFFFD, E0000-EFFFD
like image 84
Abhishek Keshri Avatar answered Nov 14 '22 23:11

Abhishek Keshri