为什么参数依赖查找发明? [英] Why was argument dependent lookup invented?
问题描述
为什么参数依赖查找(ADL)发明?就是这样我们可以写 cout<< 如果是这样的话,为什么ADL不能限于操作符而不是所有的函数?
如果C ++有其他方法可以阻止ADL的引入例如通过可变参数模板的类型安全 printf
?通过可变参数模板,通用输出内置和用户定义的类型?
对于类X,所有函数,包括自由函数,
- 提及X和
- 提供X
Why was argument dependent lookup (ADL) invented? Is it just so we can write cout << stuff
instead of std::operator<<(cout, stuff)
? If that is the case, why wasn't ADL limited to operators instead of all functions?
Could the introduction of ADL have been prevented if C++ had had some other way to do generic output of both built-in and user-defined types, for example a type-safe printf
via variadic templates?
ADL was invented to allow the Interface Principle:
The Interface Principle
For a class X, all functions, including free functions, that both
- "mention" X, and
- are "supplied with" X
are logically part of X, because they form part of the interface of X.
Check out Herb Sutter's excellent Guru of the Week on the topic.
这篇关于为什么参数依赖查找发明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!