2

I've gotten in the habit of using the new enum classes (or strong enums) in C++. Now, I need to deprecate a member.

However, the standard enum deprecation syntax, as described in the following question, does not compile. c++ mark enum value as deprecated?

This was my expectation:

enum class MyEnum {
    ORIGINAL [[deprecated("Use `PRIMARY` instead.")]] = 1,
    PRIMARY = 1,
    SECONDARY = 2,
    THIRD = 3
};

Result:

...: error: expected '}' before '[' token
     ORIGINAL [[deprecated]] = 1,
              ^

On second glance, I can see that this syntax is intended for enum MyEnum ..., as opposed to enum class MyEnum ....

I can't seem to find anything about it on CPP Reference either: https://en.cppreference.com/w/cpp/language/attributes/deprecated

Am I missing something, or what is the appropriate way to deprecate a member of an enum class?

Zak
  • 12,213
  • 21
  • 59
  • 105
  • 1
    Are you using the proper version of C++? This seems to only be standard since C++14. If you're using say C++11, it won't work. –  Jan 31 '21 at 04:01
  • Thank you, this is the answer. A dev tool had it's own toolchain, and it brought in an older version of the compiler. It was not using the compiler I was expecting. – Zak Jan 31 '21 at 05:17

1 Answers1

1

Thanks to @Chipster for the pointer! It turns out the dev tool I was using had it's own toolchain and version of the compiler, which was older.

My standard toolchain is up-to-date, but was not being invoked.

I was able to identity this buy printing the value of __cplusplus. It is an integer value of an ISO style date (201103 in my case) to represent the cpp standard version. This also allows versions to be LT/GT compared when enabling and disabling features.

Zak
  • 12,213
  • 21
  • 59
  • 105