argument-dependent-lookup

Why can an (irrelevant) using declaration reconcile overload ambiguity with Argument-Dependent Lookup?

六眼飞鱼酱① 提交于 2019-12-10 19:07:23
问题 This is a follow up of the question here on function overload with Argument-Dependent Lookup (ADL). I wanted to check my understanding of the rules under these circumstances so I wrote some test code. First, there is no swap for HasPtr class in std, of course, so I wrote a namespace of my own which contains a HasPtr version of swap in addition to the one already defined in global scope. The using declaration works as what I expected -- an error of ambiguity was produced because there is a

Nested Classes and ADL

房东的猫 提交于 2019-12-10 18:12:36
问题 Here's the code: namespace Namespace { struct L0 { enum SomeEnum { EnumVal }; struct L1 { friend void f(SomeEnum) { std::cout << "f()" << std::endl; } }; friend void g(SomeEnum) { std::cout << "g()" << std::endl; } }; } int main() { f(Namespace::L0::EnumVal); // error: f not defined g(Namespace::L0::EnumVal); // good } The idea here is to make the compiler find f() and g() through ADL. However, this code fails to compile with gcc or clang. The similar code seemed to compile fine under MSVC

Do custom container iterators guarantee ADL to consider namespace std?

∥☆過路亽.° 提交于 2019-12-10 04:11:58
问题 I have no intention of using this in real code. I promise. Does the standard guarantee that std namespace is going to be found when a function argument is of type container::iterator and container::iterator isn't a typedef for a built-in type? For example #include <set> #include <algorithm> int main() { std::set<int> s; find(s.begin(), s.end(), 0); //do I have a guarantee that std::find will be found? } In other words, can the iterator class be defined in such a namespace that std won't be

Confusion around function call resolution

旧巷老猫 提交于 2019-12-09 17:08:55
问题 This question is inspired by this one. Consider the code: namespace ns { template <typename T> void swap(T& a, T& b) { using namespace std; swap(a, b); } } After some test with GCC, I found that swap(a, b); resolves to 1) std::swap if T has overloaded std::swap (e.g., standard container types) 2) ns::swap otherwise, leading to infinite recursion. So, it seems that the compiler will first try to find a match in the namespace ns . If a match is found, the search ends. But this is not the case

Why doesn't function declared inside other function participate in argument dependent lookup?

心不动则不痛 提交于 2019-12-09 16:55:16
问题 Consider a simple example: template <class T> struct tag { }; int main() { auto foo = [](auto x) -> decltype(bar(x)) { return {}; }; tag<int> bar(tag<int>); bar(tag<int>{}); // <- compiles OK foo(tag<int>{}); // 'bar' was not declared in this scope ?! } tag<int> bar(tag<int>) { return {}; } Both [gcc] and [clang] refuses to compile the code. Is this code ill-formed in some way? 回答1: From unqualified lookup rules ([basic.lookup.unqual]): For the members of a class X , a name used in a member

Comparison operator for std::vector<T> fails to find comparison operator for T

廉价感情. 提交于 2019-12-09 09:09:09
问题 The following very simple code won't compile #include <vector> #include <string> namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ return lhs.f == rhs.f && lhs.uuid == rhs.uuid; } int main(){ std::vector<Foobar::Test> a; std::vector<Foobar::Test> b; if(a==b){ } return 0; } https://godbolt.org/g/zn6UgJ Won't compile in any of the compilers I have. While the following #include <vector> #include <string>

Why was argument dependent lookup invented?

帅比萌擦擦* 提交于 2019-12-09 03:02:26
问题 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? 回答1: ADL was invented to allow the Interface Principle: The Interface Principle For

Can't understand name lookup differences between an int and a user defined type - perhaps ADL related

ぐ巨炮叔叔 提交于 2019-12-08 16:12:12
问题 Why does the following code compile: template<typename T> void foo(T in) { bar(in); } struct type{}; void bar(type) {} int main() { foo(type()); } When the following does not: template<typename T> void foo(T in) { bar(in); } void bar(int) {} int main() { foo(42); } Compiling with GnuC++ 7: a.cpp: In instantiation of 'void foo(T) [with T = int]': a.cpp:9:20: required from here a.cpp:2:21: error: 'bar' was not declared in this scope, and no declarations were found by argument-dependent lookup

C++ operator lookup rules / Koenig lookup

杀马特。学长 韩版系。学妹 提交于 2019-12-08 14:56:12
问题 While writing a test suite, I needed to provide an implementation of operator<<(std::ostream&... for Boost unit test to use. This worked: namespace theseus { namespace core { std::ostream& operator<<(std::ostream& ss, const PixelRGB& p) { return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b << ")"); } }} This didn't: std::ostream& operator<<(std::ostream& ss, const theseus::core::PixelRGB& p) { return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b

Why does C++11 not support name lookup like this? [closed]

青春壹個敷衍的年華 提交于 2019-12-07 06:40:22
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . struct A { enum InnerEnum { X }; A(InnerEnum x) {} }; int main() { A a(X); } The compiler complains: error C2065: 'X' : undeclared