Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use a keyword as identifier in an enum?

Tags:

c++

enums

keyword

I have been unable to find if there is a way to use a keyword in a enum definition, like:

enum class EServerAction
{
    create,
    read,
    update,
    delete
};

In C# I can use the @ char to make the compiler look at it as an identifier. Is there a way to do this in C++ (Visual Studio 2015)?

like image 837
Super Rey Avatar asked Apr 11 '16 11:04

Super Rey


2 Answers

No they cant be used.

From MSDN

Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program.

The rule for identifier says:

An identifier can be used to name objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs, goto labels, and other entities, with the following exceptions:

  • the identifiers that are keywords cannot be used for other purposes;
  • the identifiers with a double underscore anywhere are reserved;
  • the identifiers that begin with an underscore followed by an uppercase letter are reserved;
  • the identifiers that begin with an underscore are reserved in the global namespace.
like image 64
Rahul Tripathi Avatar answered Oct 14 '22 16:10

Rahul Tripathi


As per 2.12 [lex.key] in the C++14 standard, certain identifiers must never be used as identifiers:

The identifiers shown in Table 4 are reserved for use as keywords (that is, they are unconditionally treated as keywords in phase 7) except in an attribute token (7.6.1) [ Note: The export keyword is unused but is reserved for future use. — end note ]:

Table 4 — Keywords

   alignas continue friend register true
   alignof decltype goto reinterpret_cast try
   asm default if return typedef
   auto delete inline short typeid
   bool do int signed typename
   break double long sizeof union
   case dynamic_cast mutable static unsigned
   catch else namespace static_assert using
   char enum new static_cast virtual
   char16_t explicit noexcept struct void
   char32_t export nullptr switch volatile
   class extern operator template wchar_t
   const false private this while
   constexpr float protected thread_local
   const_cast for public throw

Furthermore, some identifiers shall not be used:

Furthermore, the alternative representations shown in Table 5 for certain operators and punctuators (2.6) are reserved and shall not be used otherwise:

Table 5 — Alternative representations

and and_eq bitand bitor compl not
not_eq or or_eq xor xor_eq

Even furthermore, as per 2.11 Identifier [lex.name], some are illegal to use, but the compiler is not required to tell you:

some identifiers are reserved for use by C++ implementations and standard libraries (17.6.4.3.2) and shall not be used otherwise; no diagnostic is required

— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.

— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

like image 30
Sebastian Mach Avatar answered Oct 14 '22 16:10

Sebastian Mach