argument-dependent-lookup

Does argument dependent lookup only search namespaces or classes too?

偶尔善良 提交于 2019-12-21 06:18:31
问题 Ive been reading the Josuttis template book, and Ive been trying to put my head around ADL. He says "ADL proceeds by looking up the name in namespaces and classes "assocaited with" the types of the call arguments". Im just trying to see how it works looking up the name in a class. I put an example of my test below. I see how it looks up the name in a namespace. class bryan_ns { public: class bryan { public: enum E { e1 }; static void bryan_test() { std::cout << __PRETTY_FUNCTION__ << std:

Does argument dependent lookup only search namespaces or classes too?

别等时光非礼了梦想. 提交于 2019-12-21 06:17:04
问题 Ive been reading the Josuttis template book, and Ive been trying to put my head around ADL. He says "ADL proceeds by looking up the name in namespaces and classes "assocaited with" the types of the call arguments". Im just trying to see how it works looking up the name in a class. I put an example of my test below. I see how it looks up the name in a namespace. class bryan_ns { public: class bryan { public: enum E { e1 }; static void bryan_test() { std::cout << __PRETTY_FUNCTION__ << std:

Name hiding by using declaration

怎甘沉沦 提交于 2019-12-21 05:35:32
问题 #include <iostream> struct H { void swap(H &rhs); }; void swap(H &, H &) { std::cout << "swap(H &t1, H &t2)" << std::endl; } void H::swap(H &rhs) { using std::swap; swap(*this, rhs); } int main(void) { H a; H b; a.swap(b); } And this is the result: swap(H &t1, H &t2) In the code above, I try to define a swap function of H . In the function void H::swap(H &rhs) , I use an using declaration to make the name std::swap visible. If there isn't an using declaration, the code cannot be compiled

Why is this call to swap() ambiguous?

扶醉桌前 提交于 2019-12-21 03:15:26
问题 The following program #include <algorithm> #include <utility> #include <memory> namespace my_namespace { template<class T> void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template<class T, class Alloc = std::allocator<T>> class foo {}; } int main() { my_namespace::foo<int> *a, *b; using my_namespace::swap; swap(a,b); return 0; } causes both g++ and clang to issue the following compiler error on my system: $ clang -std=c++11 swap_repro.cpp -I. swap_repro

GCC, Clang, and IBM disagree on how to perform template-parameter-dependent name lookup. Which one is right?

亡梦爱人 提交于 2019-12-20 11:22:30
问题 Consider this example I found on IBM's website: #include <iostream> using namespace std; void f(double) { cout << "Function f(double)" << endl; } template<class T> void g(T a) { f(123); h(a); } void f(int) { cout << "Function f(int)" << endl; } void h(double) { cout << "Function h(double)" << endl; } void i() { extern void h(int); g<int>(234); } void h(int) { cout << "Function h(int)" << endl; } int main(void) { i(); } What will it print? The IBM documentation I adapted this example from,

Different behavior for qualified and unqualified name lookup for template

不羁岁月 提交于 2019-12-18 12:28:08
问题 How should this code behave? It calls generic function ignoring my overload if I use qualified name in call_read() function; and it calls overload first and then generic version if I use unqualified name. What's the difference? Is it a bug in GCC? #include <iostream> struct info1 {}; struct info2 {}; template<class T> void read(T& x) { std::cout << "generic" << std::endl; } template<class T> void call_read(T& x) { ::read(x); // if I replace ::read(x) with read(x) the overload is called } void

Where should I define operator >> for my specialization of std::pair?

Deadly 提交于 2019-12-18 04:00:45
问题 Consider the following program: #include <iostream> #include <iterator> #include <vector> #include <utility> using namespace std; //just for convenience, illustration only typedef pair<int, int> point; //this is my specialization of pair. I call it point istream& operator >> (istream & in, point & p) { return in >> p.first >> p.second; } int main() { vector<point> v((istream_iterator<point>(cin)), istream_iterator<point>()); // ^^^ ^^^ //extra parentheses lest this should be mistaken for a

Is ADL the only way to call a friend inline function?

一笑奈何 提交于 2019-12-18 01:52:52
问题 Let us define f , as a friend function of S , inside the declaration of S : struct S { friend void f() {} }; I cannot find a way to call f . Is it true, then, that such an inline friend function can only be called with argument-dependant lookup? struct S { friend void f() {} friend void g(S const&) {} } const s; int main() { // f(); // error: 'f' was not declared in this scope // S::f(); // error: 'f' is not a member of 'S' g(s); // S::g(s); // error: 'g' is not a member of 'S' } Bonus: what

What are the pitfalls of ADL?

非 Y 不嫁゛ 提交于 2019-12-17 03:02:33
问题 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? 回答1: 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; }

Overload operator>> for std::pair<int, int>

不打扰是莪最后的温柔 提交于 2019-12-13 11:42:43
问题 I am trying to use boost::lexical_cast on a std::pair<int, int> . #include <iostream> #include <utility> #include <boost/lexical_cast.hpp> namespace my { // When my_pair is a user defined type, this program compiles // and runs without any problems. // When declaring my_pair as an alias of std::pair<int, int>, // it fails to compile /* struct my_pair { int first; int second; }; */ using my_pair = std::pair<int, int>; std::istream& operator>>(std::istream& stream, my_pair& pair) { stream >>