1 Reply Latest reply: Jul 30, 2014 12:30 PM by Steve.Clamage-Oracle RSS

    Non-conformance to standard for one narrowing initialization syntax in Solaris Studio 12.4 beta July refresh

    1059891

      I have this C++ program:

       

      int main(int, char **)

      {

            char c('a');

            unsigned long u1 = c;

            unsigned long u2(c);

            unsigned long u3 = { c };

       

            return 0;

      }

       

      When I compile it in C++11 mode with Solaris Studio 12.4 beta July refresh I get the following two errors:

       

      % CC -std=c++11 Main.cc

      "Main.cc", line 5: Error: Narrowing conversion of 'char' value to 'unsigned long' is not allowed here.

      "Main.cc", line 6: Error: Narrowing conversion of 'char' value to 'unsigned long' is not allowed here.

      2 Error(s) detected.

       

      My understanding from reading the 4th edition of Stroustrup's "The C++ Programming Language" is that only the initializer list syntax on line 6 should treat narrowing conversions as an error.  The narrowing conversion on line 5 should not be an error.

       

      clang++ running on Mac OS X in C++11 mode agrees with me:

       

      $ clang++ -std=c++11 Main.cc

      Main.cc:6:26: error: non-constant-expression cannot be narrowed from type 'char' to 'unsigned long' in initializer list [-Wc++11-narrowing]

          unsigned long u3 = { c };

                               ^

      Main.cc:6:26: note: override this message by inserting an explicit cast

          unsigned long u3 = { c };

                               ^

                               static_cast<unsigned long>( )

      1 error generated.

       

      Note that, despite the verbosity of the error message, it is only complaining about line 6; clang++ has accepted line 5.