static const vs #define

前端 未结 11 1011
醉话见心
醉话见心 2020-11-22 13:01

Is it better to use static const vars than #define preprocessor? Or maybe it depends on the context?

What are advantages/disadvantages for

11条回答
  •  感动是毒
    2020-11-22 13:31

    Always prefer to use the language features over some additional tools like preprocessor.

    ES.31: Don't use macros for constants or "functions"

    Macros are a major source of bugs. Macros don't obey the usual scope and type rules. Macros don't obey the usual rules for argument passing. Macros ensure that the human reader sees something different from what the compiler sees. Macros complicate tool building.

    From C++ Core Guidelines

提交回复
热议问题