<> 讨论

pyl2001 2003-12-12 02:03:17
inline int const& max (int const& a, int const& b)
{
return a<b?b:a;
}

// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a<b?b:a;
}

// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c);
}

int main()
{
::max(7, 42, 68); // calls the template for three arguments
::max(7.0, 42.0); // calls max<double> (by argument deduction)
::max('a', 'b'); // calls max<char> (by argument deduction)
::max(7, 42); // calls the nontemplate for two ints
::max<>(7, 42); // calls max<int> (by argument deduction)
::max<double>(7, 42); // calls max<double> (no argument deduction)
::max('a', 42.7); // calls the nontemplate for two ints
}

书里写的和我实际在vc里运行结果不一样,并不是优先调用非template函数。
还有个地方template <typename T1, typename T2>
inline T1 max (T1 const& a, T2 const& b)
{
return a < b ? b : a;
}

max(4,4.2) // OK, but type of first argument defines return type
This may appear to be a good method to enable passing two call parameters of different types to the max() template, but in this example it has drawbacks. The problem is that the return type must be declared. If you use one of the parameter types, the argument for the other parameter might get converted to this type, regardless of the caller's intention. C++ does not provide a means to specify choosing "the more powerful type" (however, you can provide this feature by some tricky template programming, see Section 15.2.4 on page 271). Thus, depending on the call argument order the maximum of 42 and 66.66 might be the double 66.66 or the int 66. Another drawback is that converting the type of the second parameter into the return type creates a new, local temporary object. As a consequence, you cannot return the result by reference. [4] In our example, therefore, the return type has to be T1 instead of T1 const&.

[4] You are not allowed to return values by reference if they are local to a function because you'd return something that doesn't exist when the program leaves the scope of this function.


但是在这之后举的例子,作者还使用的const& 作为返回类型。
...全文
51 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
merlinran 2003-12-18
  • 打赏
  • 举报
回复
使用多个编译器,对比验证。下面是一个可以免费获取的编译器列表,我自己的机器上都装着:
1、免费的Digital Mars C++编译器
ftp://ftp.digitalmars.com/Digital_Mars_C++/Patch/dm838c.zip
2、MingW,gcc移植到Windows平台下,自由软件
http://mingw.sourceforge.net/download.shtml
3、免费的Borland C++编译器
http://www.borland.com/products/downloads/download_cbuilder.html
pyl2001 2003-12-12
  • 打赏
  • 举报
回复
This is only one example of code that might behave differently than expected as a result of detailed overload resolution rules. For example, the fact that not all overloaded functions are visible when a corresponding function call is made may or may not matter. In fact, defining a three-argument version of max() without having seen the declaration of a special two-argument version of max() for ints causes the two-argument template to be used by the three-argument version:

// basics/max4.cpp

// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a<b?b:a;
}

// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c); // uses the template version even for ints
} // because the following declaration comes
// too late:
// maximum of two int values
inline int const& max (int const& a, int const& b)
{
return a<b?b:a;
}


vc调试结果还是调用了非模版函数,才看到第2章就有这些问题无法实践验证:(。 真不知道该记住书里的还是参考调试结果
expiry 2003-12-12
  • 打赏
  • 举报
回复
marking
pyl2001 2003-12-12
  • 打赏
  • 举报
回复
说错了,::max('a', 42.7); // calls the nontemplate for two ints 是这句不能通过

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧