struct Base {
constexpr virtual int value() const { return 1; }
};
struct Derived : Base {
constexpr int value() const override { return 2; }
};
constexpr int test() {
Derived d;
Base& b = d;
return b.value();
}
static_assert(test() == ?); struct Base {
constexpr virtual int value() const { return 1; }
};
struct Derived : Base {
constexpr int value() const override { return 2; }
};
constexpr int test() {
Derived d;
Base& b = d;
return b.value();
}
static_assert(test() == ?); static_assert(test() == 1) 通过
static_assert(test() == 2) 通过
编译错误,虚函数不能是constexpr
编译错误,constexpr函数中不能使用多态

这道题你会答吗?花几分钟告诉大家答案吧!