C++17尝鲜:if constexpr

if constexpr

if constexpr 是C++17所引入的新的语法特性。它为C++语言提供了在编译期处理条件分歧的功能。

运行期 预处理 编译期
if (condition) {
statement;
} else if (condition) {
statement;
} else {
statement;
}
#if condition
statement;
#elif condition
statement;
#else
statement;
#endif
if constexpr (condition) {
statement;
} else if constexpr (condition) {
statement;
} else {
statement;
}

注意 if constexpr 语句中的判断条件必须是编译期常量。

猜你喜欢

转载自www.cnblogs.com/zwvista/p/9238273.html