Is #define banned in industry standards?

前端 未结 13 2835
长发绾君心
长发绾君心 2020-12-23 18:52

I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, <

13条回答
  •  醉酒成梦
    2020-12-23 19:31

    No, use of macros is not banned.

    In fact, use of #include guards in header files is one common technique that is often mandatory and encouraged by accepted coding guidelines. Some folks claim that #pragma once is an alternative to that, but the problem is that #pragma once - by definition, since pragmas are a hook provided by the standard for compiler-specific extensions - is non-standard, even if it is supported by a number of compilers.

    That said, there are a number of industry guidelines and encouraged practices that actively discourage all usage of macros other than #include guards because of the problems macros introduce (not respecting scope, etc). In C++ development, use of macros is frowned upon even more strongly than in C development.

    Discouraging use of something is not the same as banning it, since it is still possible to legitimately use it - for example, by documenting a justification.

提交回复
热议问题