What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

前端 未结 16 1973
刺人心
刺人心 2021-01-29 19:48

What are some C++ related idioms, misconceptions, and gotchas that you\'ve learnt from experience?

An example:

class A
{
  public: 
  char s[1024];
  cha         


        
16条回答
  •  自闭症患者
    2021-01-29 20:27

    Sometimes, headers are polluted with not behaving macro names like

    #define max(a, b) (a > b ? a : b)
    

    Which will render code invalid that uses a max function or function object called that way. An infamous example is windows.h which does exactly that. One way around it is putting parentheses around the call, which stops it from using the macro and makes it use the real max function:

    void myfunction() {
        ....
        (max)(c, d);
    }
    

    Now, the max is in parentheses and it is not counted as a call to the macro anymore!

提交回复
热议问题