一种 C++ 函数重载机制
这个机制是由张素琴等人提出并实现的, 他们写了一个 C++ 的编译系统 COC++(开发在国产机上, UNIX 操作系统环境下具有中国自己版权的 C,C++ 和 FORTRAN 语言编译系统, 这些编译系统分别满足了 ISOC90,AT&T 的 C++85 和 ISOFORTRAN90 标准).COC++ 中的函数重载处理过程主要包括两个子过程:
1, 在函数声明时的处理过程中, 编译系统建立函数声明原型链表, 按照换名规则进行换名并在函数声明原型链表中记录函数换名后的名字 (换名规则跟本文上面描述的差不多, 只是那个 int-》为哪个字符, char-》为哪个字符等等类似的差异)
有兴趣一起交流学习 c++ 的小伙伴可以加群: 941 63 6044
图附 1, 过程 1 - 建立函数链表 (说明, 函数名的编码格式为:< 原函数名 >_< 作用域换名 >< 函数参数表编码 >, 这跟 g++ 中的有点不一样)
2, 在函数调用语句翻译过程中, 访问符号表, 查找相应函数声明原型链表, 按照类型匹配原则, 查找最优匹配函数节点, 并输出换名后的名字下面给出两个子过程的算法建立函数声明原型链表算法流程如图附 1, 函数调用语句翻译算法流程如图附 2.
有兴趣一起交流学习 c++ 的小伙伴可以加群: 941636044
附 - 模板函数和普通函数构成的重载, 调用时又是如何匹配的呢?
下面是 C++ 创始人 Bjarne Stroustrup 的回答:
1)Find the set of function template specializations that will take part in overload resolution.
- )if two template functions can be called and one is more specified than the other, consider only the most specialized template function in the following steps.
- )Do overload resolution for this set of functions, plus any ordinary functions as for ordinary functions.
- )If a function and a specialization are equally good matches, the function is perferred.
- )If no match is found, the call is an error.(不会英文的话可以选择性忽略......)
来源: http://www.jianshu.com/p/34c00ea5d65f