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 ?
mgorny
October 27, 2016, 9:49am
2
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.
2. Which clang version are you using?
[1]:std::ios_base::failure - cppreference.com
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]:std::ios_base::failure - cppreference.com
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."
rnk
October 31, 2016, 5:05pm
4
Seems like a mingw-ish bug. This program works as expected in an MSVC environment. I don’t have a 32-bit mingw installation to reproduce your configuration.
Seems like a mingw-ish bug. This program works as expected in an MSVC
environment. I don't have a 32-bit mingw installation to reproduce your
configuration.
It's my error. The std::ios_base::failure exception is derived from std::runtime_error only for c++11. In VC++ c++11 mode is automaically on whereas in clang the default is c++03 mode.