Dyi*_*oul 4 c++ templates boolean constants
template <bool flag>
class foo
{
public:
int bar()
{
if(flag)
{
// stuff
}
}
};
Run Code Online (Sandbox Code Playgroud)
当编译器编译此类时,它会将flag参数替换为true或false.然后我们有if(true)(或者if(false)).然后,if子句检查常量表达式并在编译时将被删除.我能指望编译器表现得像这样吗?