Why this code compiles using g++ 5.2.1
, but fails with g++ 4.9.3
?
//exception.h
class MyError: public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
// nothing else here
//main.cpp
#include <iostream>
#include "exception.h"
int main() {}
5.2.1
compilation:
$ g++ --version
g++ 5.2.1
$ g++ -std=c++11 -c main.cpp -o main.o
$ g++ main.o -o a.out
Compilation successfull.
4.9.3
compilation:
$ g++ --version
g++ 4.9.3
$ g++ -std=c++11 -c main.cpp -o main.o
$ g++ main.o -o a.out
In file included from main.cpp:2:0:
exception.h:3:1: error: expected class-name before ‘{’ token
{
^
exception.h:5:14: error: ‘std::runtime_error’ has not been declared
using std::runtime_error::runtime_error;
....
Solution is to add #include <stdexcept>
to exception.h
Now it works with both versions.
When I remove #include <iostream>
from main.cpp, then compilation fails even with 5.2.1 version and #include <stdexcept>
is required too.
Why this code works on 5.2.1
version without including stdexcept header?
It's included in iostream
on 5.2.1
version but not in 4.9.3
version? Reading GCC changes didn't help.