gpt4 book ai didi

c++ - #if 与 if constexpr

转载 作者:行者123 更新时间:2023-12-05 02:30:39 24 4
gpt4 key购买 nike

哪个更适合编译时配置(例如调试/发布)、预处理器指令if constexpr

#define DBG

#if DBG
// some code
#endif

// --------------------------------- or

inline constexpr bool DEBUG { true };

if constexpr ( DEBUG )
{
// some code
}

最佳答案

请注意,如果 if constexpr 不是 模板的一部分,那么 if 的其他部分(例如 elses) 仍然被编译并检查有效性。

来自 cppreference :

If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated .

Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive:

关于c++ - #if 与 if constexpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71797365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com