第九章
9.2.1 ADL
如果把被调用函数的名称(如max)用圆括号括起来,也不会使用ADL。
用VS08试了试,确实如此。
要说明的是两点:
1. “(如max)”这个是译者加的,并且加的非常不合适。 因为在上文给的例子中,max本来就不需要ADL查找。需要ADL查找的是 operator <。译者给出的这个很有误导性。
2. 用圆括号括起来的是函数名称,不是函数调用,比如下面的代码是无法编译通过的。但是如果去掉lessthan两边的括号就可以编译。
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace Test
{
class CA
{
public:
CA( int a );
int data;
};
CA::CA(int a)
{
data = a;
}
bool lessthan(const CA & a, const CA & b)
{
return a.data < b.data;
}
}
template<typename T>
T const & mmax(T const & a, T const & b)
{
return (lessthan)(a,b) ? b : a;
}
using Test::CA;
int _tmain(int argc, _TCHAR* argv[])
{
CA a(2);
CA b(3);
cout<<mmax(a,b).data;
return 0;
}