首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++ Primer Fourth Edition 16.7 的疑点

2013-01-28 
C++ Primer Fourth Edition 16.7 的疑问!不知道是不是错误,请高手帮忙看看红色的部分说:模板函数和普通函

C++ Primer Fourth Edition 16.7 的疑问!
不知道是不是错误,请高手帮忙看看
红色的部分说:
模板函数和普通函数都匹配,但事实上,当我把这个函数

int compare(const char*, const char*);

注释掉之后,会报错的:

no matching function for call to 'compare(const char [6], const char [3])'
no matching function for call to 'compare(char [6], char [3])'

那就是说模板函数根本就不匹配啊!

16.7. Overloading and Function Templates
An Example of Function-Template Matching
Consider the following set of overloaded ordinary and function templates:

     // compares two objects
     template <typename T> int compare(const T&, const T&);
     // compares elements in two sequences
     template <class U, class V> int compare(U, U, V);
     // plain functions to handle C-style character strings
     int compare(const char*, const char*);

The overload set contains three functions: The first template handles simple values, the second template compares elements from two sequences, and the third is an ordinary function to handle C-style character strings.

Resolving Calls to Overloaded Function Templates
We could call these functions on a variety of types:

     // calls compare(const T&, const T&) with T bound to int
     compare(1, 0);
     // calls compare(U, U, V), with U and V bound to vector<int>::iterator
     vector<int> ivec1(10), ivec2(20);
     compare(ivec1.begin(), ivec1.end(), ivec2.begin());
     int ia1[] = {0,1,2,3,4,5,6,7,8,9};
     // calls compare(U, U, V) with U bound to int*
     // and V bound to vector<int>::iterator
     compare(ia1, ia1 + 10, ivec1.begin());
     // calls the ordinary function taking const char* parameters
     const char const_arr1[] = "world", const_arr2[] = "hi";
     compare(const_arr1, const_arr2);
     // calls the ordinary function taking const char* parameters
     char ch_arr1[] = "world", ch_arr2[] = "hi";
     compare(ch_arr1, ch_arr2);


We'll look at each call in turn:

compare(1, 0): Both arguments have type int. The candidate functions are the first template instantiated with T bound to int and the ordinary function named compare. The ordinary function, however, isn't viablewe cannot pass an int to a parameter expecting a char*. The instantiated function using int is an exact match for the call, so it is selected.



     compare(ivec1.begin(), ivec1.end(), ivec2.begin())

compare(ia1, ia1 + 10, ivec1.begin()): In these calls, the only viable function is the instantiation of the template that has three parameters. Neither the template with two arguments nor the ordinary nonoverloaded function can match these calls.

compare(const_arr1, const_arr2): This call, as expected, calls the ordinary function. Both that function and the first template with T bound to const char* are viable. Both are also exact matches. By rule 3b, we know that the ordinary function is preferred. We eliminate the instance of the template from consideration, leaving just the ordinary function as viable.

compare(ch_arr1, ch_arr2): This call also is bound to the ordinary function. The candidates are the version of the function template with T bound to char* and the ordinary function that takes const char* arguments. Both functions require a trivial conversion to convert the arrays, ch_arr1 and ch_arr2, to pointers. Because both functions are equal matches, the plain function is preferred to the template version.

C++?Primer c++
[解决办法]
好多英文.. 
  这是函数模版的重载?
[解决办法]
看了这么多分就进来了……
[解决办法]
首先肯定不会匹配3个参数的compare
然后你传递是const_arr1,那么模板推导出来的参数类型就是数组的引用
参数分别是 const char &[6] ,  const char &[3]这是2个不同的类型
int compare(const T&, const T&);要求两个参数都一致,所以无法匹配了。

compare((const char*)const_arr1, (const char*)const_arr2);

这样就能匹配上模板参数
[解决办法]
编译器是正确的,第一个函数模板匹配不上,除非改成这样。

template <typename T, typename U> int compare(const T&, const U&);

[解决办法]
我觉得书上错了,template <typename T> int compare(const T&, const T&);的模板行惨是引用类型,如果传递的是数组的话,compare的两个行参都是引用类型,要求两个数组的维度都相同,但是树上给的两个数组的维度不一样,无法用来实例化。不过把这个函数的行惨定义位非引用类型就可以了,或者把那两个数组的维度改成相同的。
[解决办法]
引用:
我觉得书上错了,template <typename T> int compare(const T&amp;, const T&amp;);的模板行惨是引用类型,如果传递的是数组的话,compare的两个行参都是引用类型,要求两个数组的维度都相同,但是树上给的两个数组的维度不一样,无法用来实例化。不过把这个函数的行惨定义位非引用类型就可以了,或者把那两个数组的维度改成相同……


你说的太对了,我本来也疑惑呢,我忘了如果形参是引用类型的话,不会将数组名转化为指针,而是直接传递数组的引用本身C++ Primer Fourth Edition 16.7 的疑点

热点排行