<> 讨论

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& 作为返回类型。
...全文
22 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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 是这句不能通过
  • 打赏
  • 举报
回复
相关推荐
发帖
工具平台和程序库
加入

2.4w+

社区成员

C/C++ 工具平台和程序库
申请成为版主
帖子事件
创建了帖子
2003-12-12 02:03
社区公告
暂无公告