title: Is it allowed to use a constexpr cast operator to use user-defined object in case label
body: <p>I'm trying to use a user-defined object in a <code>case</code> label of a <code>switch</code> using <code>constexpr</code> constructors and cast operators. This does what I want in GCC and Clang, but doesn't work without some additional help in MSVC. This leads me to wonder if what I'm trying to do is guaranteed to work by (any version of) the standard and MSVC is not compliant (yet), or I'm just lucky with G++ and Clang and MSVC is allowed to complain by the standard.</p>
  
  <p>The minimal example I came up with is as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct Foo
  {
    int Value;
  
    constexpr Foo(int val)
    : Value (val)
    {
    }
  
    constexpr operator int() const
    {
      return Value;
    }
  };
  
  struct Bar
  {
    static constexpr Foo Value = {42};
    constexpr operator int() const
    {
      return mValue;
    }
  
    constexpr Bar(int val)
    : mValue(val)
    {
    }
  private:
    Foo mValue;
  };
  
  int main()
  {
    std::vector&lt;Bar&gt; numbers = { 3, 42, 1 };
    for (auto i : numbers)
    {
      switch (i)
      //switch (static_cast&lt;int&gt;(i))  //&lt;-- Adding the static_cast makes it work in MSVC as well
      {
        case 1:
          std::cout &lt;&lt; "one" &lt;&lt; std::endl;
          break;
        case Bar::Value:
          std::cout &lt;&lt; "The answer to life, the universe and everything." &lt;&lt; std::endl;
          break;
        default:
          std::cout &lt;&lt; "Just a value: " &lt;&lt; i &lt;&lt; std::endl;
      };
    }
    return 0;
  }
  </code></pre>
  
  <p>This results in my desired output in GCC &amp; Clang:</p>
  
  <pre><code>Just a value: 3
  The answer to life, the universe and everything.
  one
  </code></pre>
  
  <p>but results in the following error in MSVC:</p>
  
  <pre><code>source_file.cpp(46): error C2051: case expression not constant
  Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
  </code></pre>
  
  <p>Adding a <code>static_cast&lt;int&gt;</code> to the switch statement gets MSVC to understand it as well. So MSVC does allow a constexpr conversion in a case label, but apparently it doesn't convert <code>i</code> to an <code>int</code>. If I understand the (latest draft) standard correctly it should convert <code>i</code> to an integral, but perhaps it chooses a different integral type than the one I'm offering a conversion to?</p>
  
  <p>I checked the behaviour with GCC, Clang &amp; MSVC via <a href="http://rextester.com/ASWUZ96609" rel="nofollow noreferrer">http://rextester.com/ASWUZ96609</a></p>