argument-dependent-lookup

Why Argument Dependent Lookup doesn't work with function template dynamic_pointer_cast

社会主义新天地 提交于 2019-12-04 09:56:59
问题 Consider the following C++ program: #include <memory> struct A {}; struct B : A {}; int main() { auto x = std::make_shared<A>(); if (auto p = dynamic_pointer_cast<B>(x)); } When compiling with MSVC 2010, I obtain the following error: error C2065: 'dynamic_pointer_cast' : undeclared identifier The error persists if auto is replaced by std::shared_ptr<A> . When I fully qualify the call with std::dynamic_pointer_cast , the program successfully compiles. Also, gcc 4.5.1 doesn't like it either:

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

自古美人都是妖i 提交于 2019-12-04 04:43:09
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? From unqualified lookup rules ([basic.lookup.unqual]): For the members of a class X , a name used in a member function body, [...], shall be declared in one of the following ways — if X is a local class or is a nested

Confusion around function call resolution

元气小坏坏 提交于 2019-12-04 04:42:00
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 when ADL comes in, in which case, std::swap is found anyway. The resolution process seems to be

Doesn't ADL looks up static member functions?

ε祈祈猫儿з 提交于 2019-12-04 04:19:41
This is follow up question from Does argument dependent lookup only search namespaces or classes too? , In which @David Rodríguez said "ADL will look in the enclosing namespace of the type, and also inside the type itself" . I may have got him wrong what he tried to say but I was trying this example: struct foo{ static void bar(foo* z){} }; int main(){ foo* z; bar(z); } It doesn't compiles, producing the error " ‘bar’ was not declared in this scope " . Is it the case that ADL doesn't considers the static member function?. I mean in the example associated class is foo so wouldn't ADL look

Why is ADL not working with Boost.Range?

拥有回忆 提交于 2019-12-04 02:53:45
问题 Considering: #include <cassert> #include <boost/range/irange.hpp> #include <boost/range/algorithm.hpp> int main() { auto range = boost::irange(1, 4); assert(boost::find(range, 4) == end(range)); } Live Clang demo Live GCC demo this gives: main.cpp:8:37: error: use of undeclared identifier 'end' Considering that if you write using boost::end; it works just fine, which implies that boost::end is visible: Why is ADL not working and finding boost::end in the expression end(range) ? And if it's

getting an element from a tuple [duplicate]

北城余情 提交于 2019-12-03 22:16:55
Possible Duplicate: Why doesn't ADL find function templates? Calling get does not seem to invoke argument dependent lookup: auto t = std::make_tuple(false, false, true); bool a = get<0>(t); // error bool b = std::get<0>(t); // okay g++ 4.6.0 says: error: 'get' was not declared in this scope Visual Studio 2010 says: error C2065: 'get': undeclared identifier Why? Nawaz It's because you attempt to explicitly instantiate get function template, by providing 0 as template argument. In case of templates, ADL works if a function template with that name is visible at the point of the call. This visible

Does argument dependent lookup only search namespaces or classes too?

流过昼夜 提交于 2019-12-03 20:36:10
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::endl; } }; void f(bryan::E) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; void f(int) { std::cout

Name hiding by using declaration

天大地大妈咪最大 提交于 2019-12-03 20:22:25
#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 because there is no usable swap function with two parameters in class H . I have a question here. In my

Beginning generically, plus decltype considering local using-declaration

谁都会走 提交于 2019-12-03 17:03:34
问题 C++0x's ranged-for loop has a special exception to handle arrays (FDIS §6.5.4), and there are two functions, std::begin and end, which are overloaded to handle arrays or to select begin/end methods. This leads me to believe a function accepting a generic sequence could be written to match a ranged-for loop's behavior: template<class C> void f(C &c) { using std::begin; using std::end; do_something_with(begin(c), end(c)); } If there's a "more specific" begin/end in the namespace of C, it will

Is it possible to take the address of an ADL function?

亡梦爱人 提交于 2019-12-03 16:12:30
问题 Is it possible to take the address of a function that would be found through ADL? For example: template<class T> void (*get_swap())(T &, T &) { return & _________; // how do I take the address of T's swap() function? } int main() { typedef some_type T; get_swap<T>(); } 回答1: Honestly, I don't know but I tend towards saying that this is not possible. Depending on what you want to achieve I can suggest a workaround. More precisely, if you just need the address of a function that has the same