先看一段代碼ios
#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; }編譯就不能經過
編譯出錯:spa
/home/zhou/C++/Re/main.cpp:36: 錯誤:call of overloaded 'RefAsParam(CRefAsParam<int, 10u>::MyArray)' is ambiguouscode
ambiguous:adj. 模糊不清的;引發歧義的編譯器
在看編譯器的認識it
../Re/main.cpp:16:17: note: static void CRefAsParam<elemType, nSize>::RefAsParam(int*) [with elemType = int, unsigned int nSize = 10u]io
../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]]編譯
重載是在編譯期完成的(多態是在運行期完成的),編譯時,選擇時不能決定,因此報錯。class