我有以下的一套模板:模板專業化
//1
template< typename T > void funcT(T arg)
{
std::cout<<"1: template< typename T > void funcT(T arg)";
}
//2
template< typename T > void funcT(T * arg)
{
std::cout<<"2: template< typename T > void funcT(T * arg)";
}
//3
template<> void funcT<int>(int arg)
{
std::cout<<"3: template<> void funcT<int>(int arg)";
}
//4
template<> void funcT< int * >(int * arg)
{
std::cout<<"4: template<> void funcT< int *>(int * arg)";
}
//...
int x1 = 10;
funcT(x1);
funcT(&x1);
是否有人可以解釋爲什麼funcT(x1);
調用函數#3和funcT(&x1);
調用函數#2,但並不#4如預期?
我已經閱讀過這篇文章http://www.gotw.ca/publications/mill17.htm,其中說「重載決議忽略專門化,僅在基本功能模板上運行」。但根據這個邏輯,funcT(x1);
應該調用功能#1而不是#3。我很困惑。
這似乎是相關的:http://www.gotw.ca/publications/mill17.htm – 2010-02-03 22:15:27
我可以給你一本真的很好的書:Addison Wesley - C++模板 - 完整指南 – erick2red 2010-02-03 22:24:32