[C++]Template Argument Deduction: automatic type conversation

1 #include <string> 2 using namespace std; 3 template <typename T> 4 inline T Add(const T& a,const T& b){ 5       return a+b; 6 } 7  8 string Add2(const string& a, const string& b) 9 {10       return a+b;11 }12 int _tmain(int argc, _TCHAR* argv[])13 {14       string a("test");15       //Add(a,"b"); //error: template parameter 'T' is ambiguous16       Add2(a,"b"); //OK for none template parameter17 18       //how to fix it.19       Add(a,string("b"));20       Add(a,static_cast<string>("b"));21       Add<string>(a,"b");22 23       return 0;24 }

原文链接: https://www.cnblogs.com/aoaoblogs/archive/2011/07/02/2096514.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27987

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午5:34
下一篇 2023年2月8日 上午5:35

相关推荐