6

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 = _π;?
4
  • 2
    Unicode characters in identifiers Commented Apr 24, 2018 at 13:51
  • 1
    I think the key element here is that the <char...> would represent the set of characters used with the literal, which essentially takes the place of the argument. So 2.5_π; would call operator "" _π() with the template arguments <'2','.','5'>. Commented Apr 24, 2018 at 14:11
  • 1
    The pi character works OK ideone.com/6AGIX0 What environment (os, shell) are you using? A6D0 doesn't look like valid UTF-8 encoding for pi. Commented Apr 24, 2018 at 14:30
  • 5
    A6 D0 is not a UTF-8 sequence. Check your editor or IDE to ensure you are saving your source file as UTF-8. Commented Apr 24, 2018 at 15:10

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.