I'm getting "was not declared in this scope" errors when trying to compile a library containing heavily templated code. It seems like the code was developed using gcc-2.95, and (as I was told) did compile about four years ago. As I found on StackOverflow and the C++-FAQ the code is indeed expected to fail, and seems to rely on non-conforming compilation.
Minimal reproduction example, compile with gcc -c example.cpp
:
// file: example.cpp
template <typename T>
class Base {
protected:
int i;
};
template <typename T>
class Derived : protected Base<T> {
public:
void f() {
// i = 5; // Error: i was not declared in this scope
this->i = 5; // compiles
}
};
My question is: How do I compile this library using more recent compilers?
The library code uses the faulty syntax (i=5) all over the place, so I'd prefer to avoid manually changing the whole library to 'this->i=5'. The provided links suggest 'using' statements, but while that would reduce my work, my preferred method would involve no code changes, but forcing the compiler into the old behaviour of 'assuming this' on these variables. I could not find a compiler-switch for this situation, -std=c++98
, -fpermissive
etc. didn't work.
Quote from the C++-FAQ:
Perhaps surprisingly, the following code is not valid C++, even though some compilers accept it:
I also tried using CLang++, with the same results. Which compilers would accept this code without changes?
Edit: Added info on -fpermissive, as Brian suggested