c – 在switch中使用constexp成员函数
老栗子 2019-09-12 01:29:17 为什么我可以在switch中使用constexp非成员函数,但是当我使用成员函数时,编译器会说:“’y’的值在常量表达式中不可用”?
class Test {
public:
constexpr Test(int i) : i(i) { }
constexpr int get() {return i;};
private:
int i;
};
constexpr int test()
{
return 1;
}
int main() {
int x = 0;
Test y = Test(4);
switch (x) {
case test(): // this is OK
break;
case y.get(): // not working
break;
}
}