在阅读卡尔森的超越C++标准作者在类的主体限定的朋友函数intrusive_ptr_add_ref reference_counted(见第36页)。该函数在适当的时候使用参数相关查找自动调用。调用结构中定义的朋友函数需要前向声明?
从未见过在类的主体中定义的朋友函数,我玩过并发现如果不使用ADL查找,gcc 4.4.3需要前向声明。事实上,似乎没有办法在没有该前向声明的情况下引用adl_no。这是C++标准的一部分还是gcc的人工产物? (我没有Windows盒子,所以不能尝试VC)。
#include <cstdlib>
#include <iostream>
namespace {
void adl_no(); // Remove this and it won't compile with gcc
struct Q {
friend void adl_yes(const Q&) {
std::cout << "adl_yes" << std::endl;
}
friend void adl_no() {
std::cout << "adl_NO" << std::endl;
}
};
}
int main(int argc, char** argv)
{
adl_yes(Q());
adl_no();
return EXIT_SUCCESS;
}
Comeau在线行为与gcc匹配,因此不太可能是错误的。没有为您搜索标准,因为我认为您可以尽可能轻松地做到这一点...... ;-P。 – 2010-08-29 00:33:58