问题描述
前一段时间,我阅读了一篇文章,解释了参数依赖查找的几个陷阱,但我找不到它了。它是关于访问你不应该访问的东西或类似的东西。所以我想我会问这里:ADL的陷阱是什么?
Some time ago I read an article that explained several pitfalls of argument dependent lookup, but I cannot find it anymore. It was about gaining access to things that you should not have access to or something like that. So I thought I'd ask here: what are the pitfalls of ADL?
推荐答案
有一个巨大的问题,抬头。例如,考虑以下实用程序:
There is a huge problem with argument-dependent lookup. Consider, for example, the following utility:
#include <iostream>
namespace utility
{
template <typename T>
void print(T x)
{
std::cout << x << std::endl;
}
template <typename T>
void print_n(T x, unsigned n)
{
for (unsigned i = 0; i < n; ++i)
print(x);
}
}
很简单,对吗?我们可以调用 print_n()
并传递任何对象,它会调用 print
打印对象 n
次。
It's simple enough, right? We can call print_n()
and pass it any object and it will call print
to print the object n
times.
事实证明,如果我们只看这个代码, / em>什么函数将被 print_n
调用。它可能是这里给出的 print
函数模板,但它可能不是。为什么?参数依赖查找。
Actually, it turns out that if we only look at this code, we have absolutely no idea what function will be called by print_n
. It might be the print
function template given here, but it might not be. Why? Argument-dependent lookup.
例如,假设你写了一个类来表示独角兽。由于某些原因,你还定义了一个名为 print
的函数(这是巧合!),它只是通过写一个解引用的空指针来引起程序崩溃你这样做;这不重要):
As an example, let's say you have written a class to represent a unicorn. For some reason, you've also defined a function named print
(what a coincidence!) that just causes the program to crash by writing to a dereferenced null pointer (who knows why you did this; that's not important):
namespace my_stuff
{
struct unicorn { /* unicorn stuff goes here */ };
std::ostream& operator<<(std::ostream& os, unicorn x) { return os; }
// Don't ever call this! It just crashes! I don't know why I wrote it!
void print(unicorn) { *(int*)0 = 42; }
}
接下来,你写一个小程序创建一个独角兽,次:
Next, you write a little program that creates a unicorn and prints it four times:
int main()
{
my_stuff::unicorn x;
utility::print_n(x, 4);
}
编译此程序,运行它,...崩溃。 什么?!没有办法,你说:我只是调用 print_n
,它调用 print
独角兽四次!是的,这是真的,但它没有调用 print
函数,你希望它调用。 my_stuff :: print
。
You compile this program, run it, and... it crashes. "What?! No way," you say: "I just called print_n
, which calls the print
function to print the unicorn four times!" Yes, that's true, but it hasn't called the print
function you expected it to call. It's called my_stuff::print
.
为什么是 my_stuff :: print
选择?在名称查找期间,编译器看到调用 print
的参数是类型 unicorn
,它是一个类类型在命名空间 my_stuff
中声明。
Why is my_stuff::print
selected? During name lookup, the compiler sees that the argument to the call to print
is of type unicorn
, which is a class type that is declared in the namespace my_stuff
.
由于参数相关的查找,编译器在搜索候选函数 print
时包含此命名空间。它找到 my_stuff :: print
,然后在重载解析过程中将其选为最佳可行候选项:不需要转换来调用候选 print
函数和非模板函数优先于函数模板,因此非模板函数 my_stuff :: print
是最佳匹配。
Because of argument-dependent lookup, the compiler includes this namespace in its search for candidate functions named print
. It finds my_stuff::print
, which is then selected as the best viable candidate during overload resolution: no conversion is required to call either of the candidate print
functions and nontemplate functions are preferred to function templates, so the nontemplate function my_stuff::print
is the best match.
(如果你不相信这一点,你可以按原样编译这个问题中的代码,看看ADL在操作。)
(If you don't believe this, you can compile the code in this question as-is and see ADL in action.)
是的,参数依赖查找是C ++的一个重要特性。基本上需要实现某些语言特性的期望行为,如重载运算符(考虑流库)。也就是说,它也非常,非常有缺陷,可以导致真正丑陋的问题。已经有几个建议来修正依赖于参数的查找,但是没有一个被C ++标准委员会接受。
Yes, argument-dependent lookup is an important feature of C++. It is essentially required to achieve the desired behavior of some language features like overloaded operators (consider the streams library). That said, it's also very, very flawed and can lead to really ugly problems. There have been several proposals to fix argument-dependent lookup, but none of them have been accepted by the C++ standards committee.
这篇关于ADL的缺陷是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!