I want to use t(rvalue reference) in a case of switch(T), but I get an error the value of 't' is not usable in a constant expression
. How to do it the right way.
#include <iostream>
using namespace std;
int main(){
int (&&t)=5;
int T{};
switch(T){
case t: // error in this case
cout<<t<<endl;
break;
default:
cout<<"default"<<endl;
break;
}
}
You can't use an
rvalue reference
as a case label, since it is not a constant expression. You can however use aconst int
variable:这可能应该是:
也一样
请注意,尝试执行以下操作:
will not work either, since the initializer is not a constant expression, so there is no way that I'm aware of that lets you use an
rvalue reference
as a case label.