4

I am working on compiling a very old piece of legacy code with g++ 4.4.7. All I really know about this code is that it was developed on a Irix/Sun system meaning it had a MIPS architecture. One rather odd thing I found when working with this code is that it sometimes calls function like endl and set_new_handler without the std:: prefix. Obviously this results in a compilation error. Since I am working under the assumption that this piece of code compiled on some machine at some time, I am a bit wary about blindly adding the std:: prefix to make it compile since It may change the behavior.

So, is there some old non-ISO compiler that allowed this piece of code to compile? Or is there some sort of flag that I can pass to gcc that would allow this piece of code to work?

Iliketoproveit
  • 445
  • 6
  • 15
  • 2
    My guess is Turbo-C++, which did not implement namespaces. Also, good luck :| – Quentin Dec 22 '17 at 14:50
  • @Ron I understood that g++ is what OP is using *today* to compile that unknown code. – Quentin Dec 22 '17 at 14:52
  • 2
    I see. My bad, I stand corrected. A reminder to myself: _Drink two cups of coffee prior to answering_. – Ron Dec 22 '17 at 14:55
  • Yes, all we know is that it ran on Solaris which I don't believe has gcc – Iliketoproveit Dec 22 '17 at 14:56
  • I don't think the `std` namespace was part of C++ until the STL was folded into the standard, but I can't back that up with any references. – Mark Ransom Dec 22 '17 at 14:57
  • 2
    Found one from 2000: http://www.gotw.ca/publications/migrating_to_namespaces.htm – Mark Ransom Dec 22 '17 at 15:00
  • 2
    My company used gcc on Solaris. Regardless, the expression "software rots" applies in this situation. Old code that has not been maintained "rots" due to things like the platform or the language evolving. Commonly, the only languages that evolve over the decades are the ones that are being used... and C++ has achieved extraordinary success by this metric. – Eljay Dec 22 '17 at 15:05
  • @MarkRansom great find, this probably explains why I'm finding stuff like this written in the code. It doesn't mention a specific compiler though, was this a feature that ISO standardized near the turn of the millennium? – Iliketoproveit Dec 22 '17 at 15:08
  • @Eljay Do you have pointers into how to approach this? – Iliketoproveit Dec 22 '17 at 15:09
  • Might have been C++98 which made this change. I was hoping the link I found would be more specific. – Mark Ransom Dec 22 '17 at 15:09
  • 2
    @roscoe • I run into this kind of problem all the time. Where the problem is "this C++ source code is from circa 1990, and I need to get it to compile using C++17". The approach that I take is to roll up my sleeves and refactor the code kicking and screaming into Modern C++. (The code is kicking and screaming, while I'm crying and sobbing.) No easy workarounds. And I consider getting an old compiler to compile the old source code to be cheating, and likely not feasible. – Eljay Dec 22 '17 at 15:13
  • I mean you can do `using namespace std`, for all it's drawbacks, that's probably the simplest way to get past these errors toward building. But if you're looking to maintain the solution, then you'll need to either try a replace all: `s/endl/std::endl/` or prepend the namespace on a case by case basis. – Jonathan Mee Dec 22 '17 at 15:29
  • Namespaces in general were added during standardisation (the first C++ standard was in 1998). Prior to that the de facto standard was the ARM (Annotated Reference Manual). The blurb for that says "A final chapter describes resolutions by the ANSI/ISO committee including new features such as ... namespaces." (E.g. https://www.amazon.com/Annotated-C-Reference-Manual/dp/0201514591) – Alan Stokes Dec 22 '17 at 15:45

1 Answers1

2

The std namespace wasn't introduced into C++ until the first ISO/IEC standard in 1998 (often referred to as C++98). Prior to that time all of the standard library functions and objects were part of the global namespace.

Herb Sutter wrote a piece called Migrating to Namespaces in 2000 detailing his advice on making the transition.

I am not aware of any compiler flags that would fold the std namespace into the global one, and it would be a bad idea anyway - std is much larger today than when it was first introduced, and name collisions would be almost certain. See Why is “using namespace std” considered bad practice?

It is unlikely that you would have a collision with names that were part of the standard library prior to 1998, so it should be safe to pull those names individually into the global namespace. If you are using precompiled headers you can put the using directives in there after including the standard headers that define the symbols and fix the entire project silently. Just add a line for each compiler error you run across.

using std::endl;
using std::set_new_handler;

I would only advise this if your goal is to get the code up-and-running in as little time and effort as possible. The better long-term solution is still to put std:: in front of all the names from the library.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622