为什么要从 C++ 中删除 overload 关键字?
why overload keyword removed from C++?
我正在阅读 this。 @NPE 给出的答案谈到了有趣的历史事实并说,
- 在早期的 C++ 中,曾经有一个特殊的关键字(重载)
用于将标识符声明为重载;
那么,删除 overload 关键字的原因是什么?我没有 Stroustrup 的 Design and Evolution of C++
。 overload关键字有什么问题?
Stroustrup 在 D&E 11.2.4 "The overload
Keyword" 中提到 overload
关键字会在 "merging"(或使用)两个使用相同函数名称(没有命名空间* ).例如,C header math.h
中的 sqrt
与 C++ complex
header 中的 sqrt(complex)
相对。如果其中一个将函数声明为 overloadable,但另一个没有,则必须按照这样的顺序包含 headers,以便在重载发生之前启用:
// #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);
可能的解决方法是 "unmanageable in all but the simplest cases"。
(*) overload
关键字在 1989 年发布的 CFront 2.0 中已过时。命名空间在 1993 年被引入标准化提案。
关键字的初衷是为了应对两种恐惧:
- Concern that undetected ambiguities could occur.
- 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.
我正在阅读 this。 @NPE 给出的答案谈到了有趣的历史事实并说,
- 在早期的 C++ 中,曾经有一个特殊的关键字(重载) 用于将标识符声明为重载;
那么,删除 overload 关键字的原因是什么?我没有 Stroustrup 的 Design and Evolution of C++
。 overload关键字有什么问题?
Stroustrup 在 D&E 11.2.4 "The overload
Keyword" 中提到 overload
关键字会在 "merging"(或使用)两个使用相同函数名称(没有命名空间* ).例如,C header math.h
中的 sqrt
与 C++ complex
header 中的 sqrt(complex)
相对。如果其中一个将函数声明为 overloadable,但另一个没有,则必须按照这样的顺序包含 headers,以便在重载发生之前启用:
// #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);
可能的解决方法是 "unmanageable in all but the simplest cases"。
(*) overload
关键字在 1989 年发布的 CFront 2.0 中已过时。命名空间在 1993 年被引入标准化提案。
关键字的初衷是为了应对两种恐惧:
- Concern that undetected ambiguities could occur.
- 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.