#elseif vs #elif(C/C++预处理器)

Jer*_*ler 7 c++ conditional-compilation c-preprocessor

我发现写作

#ifdef ...
#elseif defined(...)
#else
#endif
Run Code Online (Sandbox Code Playgroud)

总是导致使用#ifdef或#else条件,而不是#elseif.但是替换#elif会导致它根据定义的内容按预期工作.#elseif的存在是否有什么令人费解的目的?如果没有,为什么预处理器不抱怨?

也许这就是为什么多年(几十年,真的),我一直在使用丑陋的#else/#endif块,因为至少它们是可靠的!

Yex*_*exo 12

#elseif没有定义.预处理器不会抱怨,因为您#ifdef的错误,并且#ifdef不会解析该块中的指令.为了说明它,此代码有效:

#if 0
#random nonsense
#else
// This must be valid
#endif
Run Code Online (Sandbox Code Playgroud)