Why do people use #ifdef for feature flag tests?

后端 未结 2 1580
-上瘾入骨i
-上瘾入骨i 2021-02-13 04:30

People recommend #ifdef for conditional compilation by a wide margin. A search for #ifdef substantiates that its use is pervasive.

Yet #ifdef NAME (or equi

2条回答
  •  旧巷少年郎
    2021-02-13 04:59

    Why do people still use #ifdef in this scenario?

    Personal opinion: it's marginally easier to control from the command line. I prefer -DOPTION over -DOPTION=1.

    Also, existence of a name is clearly binary. I don't have to be able to handle {0, non-zero, undefined}.

    Are they simply unaware that #if works perfectly fine on undefined names?

    I wasn't aware. What are the semantics of this? Is an undefined name assumed to be 0? Do I want to have to explain that to the guy who barely understands the preprocessor to begin with?

    Or is there an actual disadvantage to #if vs #ifdef for conditional compilation?

    To me, the binary nature of #ifdef/#ifndef of name existence is a clarity benefit. Also, my primary usage of either construct is for include guards. That pattern is cleanest with #ifndef.

提交回复
热议问题