Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why overload keyword removed from C++?

I was reading this. The answer given by @NPE talks about interesting historical facts & says that,

  • In early C++ there used to be a special keyword (overload) that had to be used to declare an identifier as overloaded;

So, What is the reason behind removal of overload keyword? I don't have Design and Evolution of C++ by Stroustrup. What's wrong with overload keyword?

like image 356
Destructor Avatar asked Jun 26 '15 07:06

Destructor


1 Answers

Stroustrup mentions in D&E 11.2.4 "The overload Keyword" that the overload keyword causes troubles when "merging" (or using) two libraries which both used the same function name (without namespaces*). For example, sqrt in the C header math.h versus the sqrt(complex) from the C++ complex header. If one of them declared the function as overloadable, but the other did not, you had to include the headers in such an order that overloading was enabled before it occurred:

// #include <complex>
overload sqrt;
complex sqrt(complex);

// #include <math.h>
double sqrt(double);   // fine

// ---------------------------

// #include <math.h>
double sqrt(double);

// #include <complex>
overload sqrt;         // ERROR: cannot allow overloading
                       //        of an already declared function
complex sqrt(complex);

The possible workarounds are "unmanageable in all but the simplest cases".

(*) The overload keyword was made obsolete with CFront 2.0, released in 1989. Namespaces were introduced into the standardization proposal in 1993.

The original intent of the keyword was to deal with two fears:

  1. Concern that undetected ambiguities could occur.
  2. Concern that a program could not properly be linked unless the programmer explicitly declared which functions were supposed to be overloaded.

The former fear proved largely groundless. The few problems found in actual use are dealt with by the order-independent overloading resolution rules. The latter fear proved to have no basis in a general problem with C separate compilation rules that had nothing to do with overloading.

like image 157
dyp Avatar answered Nov 07 '22 01:11

dyp