3

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;
    }
}
max66
  • 65,235
  • 10
  • 71
  • 111
  • What is your question? Why it doesn't compile with VS15, how to make it work with VS, who is right, ...? – Rakete1111 Dec 19 '16 at 17:54
  • Edited the question. – Jean-Marc Lefébure Dec 19 '16 at 18:05
  • 1
    It compiles just fine online on http://webcompiler.cloudapp.net/, which uses a more up-to-date version of VC++, so that leads me to think it's simply a bug or not-yet-implemented feature in VS2015, but an answer should explain precisely what it is that is buggy or not yet implemented. @Rakete1111 FYI, VS2015 is known also as VS14. VS2017 is known also as VS15. Shortening VS2015 to VS15 is confusing. –  Dec 19 '16 at 18:13
  • @hvd Yeah, I guess you're right :) – Rakete1111 Dec 19 '16 at 18:15
  • 2
    It would be handy to have the error message that the compiler is returning. – Mikel F Dec 19 '16 at 18:17
  • I just tested using VS2015 and it works. But I have applied the patches as of a few weeks ago. They have done some work on `constexpr`. But sadly, hana still won't work. – lakeweb Dec 19 '16 at 18:24
  • Hey, I had made something similar if you want to check it out: http://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c-and-future-c17-c20/40198772#40198772 – user Dec 19 '16 at 18:38
  • On a side note, shouldn't `id` and `name[]` be declared as `const`? – Remy Lebeau Dec 19 '16 at 20:47

0 Answers0