[C++]Template Argument Deduction: automatic type conversation is not allowed

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 ambiguous
16 Add2(a,"b"); //OK for none template parameter
17
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 }
posted @ 2011-07-02 22:55  嗷嗷  阅读(301)  评论(0编辑  收藏  举报