[cfe-dev] Odd clang result using stringstream formatted input

Edward Diener via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 27 06:40:59 PDT 2016


On 10/27/2016 5:49 AM, Michał Górny via cfe-dev wrote:
> On Thu, 27 Oct 2016 05:41:12 -0400
> Edward Diener via cfe-dev <cfe-dev at lists.llvm.org> wrote:
>
>> The following program:
>>
>> #include <iostream>
>> #include <sstream>
>> #include <stdexcept>
>> int main()
>>      {
>> 	
>>      std::stringstream ss;
>>      ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
>>      char c;
>>
>>      try
>>          {
>>          ss >> c;
>>          std::cout << "Exception not thrown.";
>>          }
>>      catch (std::runtime_error &)
>>          {
>>          std::cout << "Exception std::runtime_error thrown.";
>>          }
>>      catch (...)
>>          {
>>          std::cout << "Unknown exception thrown.";
>>          }
>>      }
>>
>> outputs "Unknown exception thrown." when compiled and linked with the
>> latest clang built from source on Windows 7. The compilation options are:
>>
>> -c -x c++ -Wno-unused-local-typedef -Wno-dll-attribute-on-redeclaration
>> -O0 -g -fno-inline -Wall -g -march=i686 -m32 -o
>>
>> The linker options are:
>>
>> -Wl,-Bstatic  -Wl,-Bdynamic  -Wl,--end-group -g -march=i686 -m32
>>
>> I would have expected the output to be "Exception std::runtime_error
>> thrown." as it is when compiled with VC++ 14.
>>
>> Is clang following the C++ standard or is this a bug in clang ?
>
> I can't reproduce this on Linux with libc++, using quite recent SVN
> versions.
>
> 1. Could you try with -std=c++11? Looking at [1], that particular
> inheritance is specific to C++11 and newer.

Same result with -std=c++11.

>
> 2. Which clang version are you using?

Built from source so it would be 4.0.

>
> [1]:http://en.cppreference.com/w/cpp/io/ios_base/failure

Thanks I see that now. However if the above is changed to adding:

     catch (std::ios_base::failure &)
         {
         std::cout << "Exception std::ios_base::failure thrown.";
         }

before the:

     catch (std::runtime_error &)
         {
         std::cout << "Exception std::runtime_error thrown.";
         }

in the example above, the output is still

"Unknown exception thrown."





More information about the cfe-dev mailing list