Can C++ variables in cpp file defined as Special Symbols β

前端 未结 3 431
太阳男子
太阳男子 2021-01-21 14:11

Can we define the variable in c++/ c using special characters such as;
double ε,µ,β,ϰ;

If yes, how can this be achieved?

相关标签:
3条回答
  • 2021-01-21 14:38

    Yes you can use special characters, but not all of them. You can find the allowed one in the link below.

    You can find a detailed explanation on how to built identifier (with the list of unicode authorized characters) on the page Identifiers - cppreference.com.

    An identifier is, quoting,

    an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (see below for details). A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character). Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

    Furthermore, Unicode characters need to be escaped.

    0 讨论(0)
  • 2021-01-21 14:56

    As per the working draft of CPP standard (N4713),

    5.10 Identifiers [lex.name]
    ...
    An identifier is an arbitrarily long sequence of letters and digits. Each universal-character-name in an identifier shall designate a character whose encoding in ISO 10646 falls into one of the ranges specified in Table 2. The initial element shall not be a universal-character-name designating a character whose encoding falls into one of the ranges specified in Table 3.

    And when we look at table 3:

    Table 3 — Ranges of characters disallowed initially (combining characters)

    0300-036F 1DC0-1DFF 20D0-20FF FE20-FE2F
    

    The symbols you have mentioned are the Greek Alphabet which ranges from U+0370 to U+03FF and the extended Greek set ranges from U+1F0x to U+1FFx as per wikipedia. Both these ranges are allowed as the initial element of an identifier.

    Note that not all compilers provide support for this.

    GCC 8.2 with -std=c++17 option fails to compile.
    However, Clang 7.0 with -std=c++17 option compiles.

    Live Demo for both GCC and Clang

    0 讨论(0)
  • 2021-01-21 15:00

    Since the question is tagged Visual Studio: Just write the code as you'd expect it.

    double β = 0.1;

    When you save the file, Visual Studio will warn you that it needs to save the file as Unicode. Accept it, and it works. AFAICT, this also works in C mode, even though most other C99 extensions are unsupported in Visual Studio.

    However, as of g++ 8.2, g++ still does not support non-ASCII characters used directly in identifiers, so the code is then effectively not portable.

    0 讨论(0)
提交回复
热议问题