Why should I prefer static constexpr int in a class over enum for class-level integral constants?

后端 未结 3 1353
我寻月下人不归
我寻月下人不归 2021-02-14 08:18

C++17 Update: static constexpr variables are implicitly inline so there\'s no external definition necessary.


Original qu

3条回答
  •  萌比男神i
    2021-02-14 08:48

    One difference is that you can take the address of a static constexpr but not of an enum.

    Another is that constexpr isn't supported by older versions of the language (it was introduced in C++11).

    I'd use enum only if the values belong together. I'd also give the enum a name that describes that relationship. I wouldn't use an enum for defining unrelated constants.

提交回复
热议问题