argument-dependent-lookup

What are the pitfalls of ADL?

亡梦爱人 提交于 2019-11-26 16:05:27
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); } } It's

Interesting behavior of compiler with namespaces

允我心安 提交于 2019-11-26 13:59:11
问题 Assume the following code: #include <iostream> using namespace std; namespace X { class A{}; void f(A a){} void g(int a){} } int main() { X::A a; f(a); g(5); } When I compile the code, the following compile error occurs: main.cpp: In function 'int main()': main.cpp: error: 'g' was not declared in this scope So the function f is compiled perfectly, but g isn't. How? Both of them belong to the same namespace. Does the compiler deduce that function f belongs to the X namespace from the argument

Why doesn&#39;t ADL find function templates?

試著忘記壹切 提交于 2019-11-26 04:21:32
What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; template<int i> void frob(foo const&) {} void non_template(foo const&) {} } int main() { ns::foo f; non_template(f); // This is fine. frob<0>(f); // This is not. } This part explains it: C++ Standard 03 14.8.1.6 : [Note: For simple function names, argument dependent lookup (3.4.2) applies even when the function name is not visible within the scope of the

Why doesn&#39;t ADL find function templates?

微笑、不失礼 提交于 2019-11-26 01:15:44
问题 What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; template<int i> void frob(foo const&) {} void non_template(foo const&) {} } int main() { ns::foo f; non_template(f); // This is fine. frob<0>(f); // This is not. } 回答1: This part explains it: C++ Standard 03 14.8.1.6 : [Note: For simple function names,

What is “Argument-Dependent Lookup” (aka ADL, or “Koenig Lookup”)?

你说的曾经没有我的故事 提交于 2019-11-25 22:17:38
问题 What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well. Preferably I\'d like to know: Why is it a good thing? Why is it a bad thing? How does it work? 回答1: Koenig Lookup , or Argument Dependent Lookup , describes how unqualified names are looked up by the compiler in C++. The C++11 standard § 3.4.2/1 states: When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not considered during the usual