std::is_same<A,B>::value == 第 1 行为 true,第 2 行为 false

std::is_same<A,B>::value == true in line 1, false in line 2

本文关键字:行为 is true false value std gt lt same      更新时间:2023-10-16

我的代码在一行中返回true,另一行返回false。

例如,

struct Z{
    static const int value = 10;
};
struct A : Z{
};
struct B : Z{
};
int main(){
    if(std::is_same<A,B>::value){
        static_assert(std::is_same<A,B>::value , "why am i here?");
    }
    return 0;
}

有人可以解释一下为什么会引发静态断言错误?

这是因为 static_assert是静态(即:编译时间)断言。它不在乎上面的if语句(在运行时评估)。