DawidPi DawidPi - 1 month ago 17
C++ Question

Why is C++17 adding attributes to namespaces and enumerators?

As in the question states, I know there are going to be attributes for namespaces and enumerators in C++17. How will this influence our code? What are those attributes and what will they allow us to do? I could not find any good resource for understanding this new feature.

Answer

Currently, If you were going to depreciate a particular enum value, you would need to rely on compiler extensions. For example, in clang, you would specify deprecated enum values the following way:

enum OperationMode {
  OM_Invalid,
  OM_Normal,
  OM_Terrified __attribute__((deprecated)),
  OM_AbortOnError __attribute__((deprecated)) = 4
};

Once enums and namespaces support attributes, there will be a standard cross-compiler way of implementing similar functionality:

enum OperationMode {
  OM_Invalid,
  OM_Normal,
  OM_Terrified [[deprecated("re-named to invalid")]],
  OM_AbortOnError  [[deprecated("exceptions are used instead")]] = 4
};

Other attributes may one day find relevance to namespaces and enumerated values, but as the proposal writer states:

This paper proposes resolving these issues by allowing attributes to be specified on enumerators and namespaces, and extends the [[deprecated]] attribute to apply to these entities, as was originally intended.