constexpr as switch condition

1k views Asked by At

I am trying to design a class that behaves like an enumerated type when instantiated as constexpr, so it can for instance be used as a condition inside a switch statement. The example compiles fine with gcc 4.8.3. Why does it not compile in MSVC 2015 without modification?

#include <iostream>
using namespace std;
struct test{
    int id;
    char name[10];
    constexpr operator int() const{
        return id;
    }
};
int main(){
    constexpr test a{0,"hello"},b{1,"bonjour"},c{2,"ola"};
    auto k=b;
    #ifdef WIN32
    switch(k.id){
    #else
    switch(k){
    #endif
        case a:cout<<a.name<<endl;break;
        case b:cout<<b.name<<endl;break;
        case c:cout<<c.name<<endl;break;
        default:break;
    }
}
0

There are 0 answers