C++函数重载无聊2

来源:互联网 发布:技嘉主板设置网络唤醒 编辑:程序博客网 时间:2024/05/17 22:02

                  先看一段代码

#include <iostream>using namespace std;template<typename elemType, size_t nSize>class CRefAsParam{public:    typedef elemType MyArray[nSize];    static void RefAsParam(int p[]) {           }    static void RefAsParam(MyArray & aRR) {            }};int main(){    typedef CRefAsParam<int, 10> MyType;    MyType::MyArray a = {20, 30, 77};    MyType::RefAsParam(a);    return 0;}
编译就不能通过

编译出错:

/home/zhou/C++/Re/main.cpp:36: 错误:call of overloaded 'RefAsParam(CRefAsParam<int, 10u>::MyArray)' is ambiguous

ambiguous:adj. 模糊不清的;引起歧义的

在看编译器的认识

../Re/main.cpp:16:17: note: static void CRefAsParam<elemType, nSize>::RefAsParam(int*) [with elemType = int, unsigned int nSize = 10u]

../Re/main.cpp:21:17: note: static void CRefAsParam<elemType, nSize>::RefAsParam(elemType (&)[nSize]) [with elemType = int, unsigned int nSize = 10u, CRefAsParam<elemType, nSize>::MyArray = int [10]]

重载是在编译期完成的(多态是在运行期完成的),编译时,选择时不能决定,所以报错。




原创粉丝点击