<div class="gmail_quote">On Mon, Feb 13, 2012 at 9:43 PM, Bob Wilson <span dir="ltr"><<a href="mailto:bob.wilson@apple.com">bob.wilson@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
When you compile with -std=c++11 for 32-bit targets, clang complains about narrowing types in switch cases:<br>
<br>
$ cat t1.cpp<br>
int foo(unsigned long x) {<br>
  switch (x) {<br>
  case 1: return 2;<br>
  case -1: return 0;<br>
  }<br>
  return 1;<br>
}<br>
$ clang++ -arch i386 -c t1.cpp -std=c++0x<br>
t1.cpp:4:8: error: case value evaluates to -1, which cannot be narrowed to type<br>
      'unsigned long'<br>
  case -1: return 0;<br>
       ^<br>
1 error generated.<br>
<br>
The corresponding code written with if-statements does not get errors, and clang only warns if you enable -Wsign-compare:<br>
<br>
$ cat t2.cpp<br>
int foo(unsigned long x) {<br>
  if (x == 1) return 2;<br>
  else if (x == -1) return 0;<br>
  else return 1;<br>
}<br>
$ clang++ -arch i386 -c t2.cpp -std=c++0x<br>
$<br>
<br>
There is no error when compiling for x86_64 or if you don't specify -std=c++11.  I think this error was introduced recently -- Howard just changed libc++ to avoid it in svn 150082.  Is this error mandated by the c++11 standard or is this something we could relax?  We're seeing regressions building some code due to this error (<rdar://problem/10820369>).<br>
</blockquote><div> </div></div>This error is mandated by the C++11 standard; this is one of the four places where you are not permitted narrowing conversions in C++11 (these are: braced initializers, case values, enumerators with fixed underlying types, and non-type template arguments). We saw some compile failures due to this change too, but far fewer than were introduced by the narrowing restrictions for braced initializers.<div>
<br></div><div>- Richard<br></div>