argument-dependent-lookup

Using equality operators with boost::optional

旧巷老猫 提交于 2019-12-07 04:20:26
问题 I'm trying to define an equality operator for a type T defined in another namespace, and then use the equality operator on optional<T> . On clang (Apple LLVM 9.1.0), this code: namespace nsp { struct Foo { }; } bool operator==(const nsp::Foo& a, const nsp::Foo& b); void foo() { optional<nsp::Foo> a = none; optional<nsp::Foo> b = none; if (a == b) ; } Results in an error: /usr/local/include/boost/optional/detail/optional_relops.hpp:29:34: error: invalid operands to binary expression ('const

Argument-dependent lookup — when is it done, what is searched, and how can you force (or prevent) it?

六眼飞鱼酱① 提交于 2019-12-07 04:01:17
问题 I'm having trouble understanding the rules behind argument-dependent (Koenig) lookup. Consider the code below: #include <iostream> using namespace std; namespace adl { struct Test { }; void foo1(Test const &) { cout << "ADL used (foo1)" << endl; } void foo2(Test const &) { cout << "ADL used (foo2)" << endl; } void foo3(Test const &) { cout << "ADL used (foo3)" << endl; } } struct foo1 { foo1() { } template<class T> foo1(T const &) { cout << "ADL not used (foo1)" << endl; } template<class T>

why move swap_impl in boost::swap to a separate namespace?

眉间皱痕 提交于 2019-12-06 05:10:01
I am looking into boost::swap implementation: namespace boost_swap_impl { template<class T> BOOST_GPU_ENABLED void swap_impl(T& left, T& right) { using namespace std;//use std::swap if argument dependent lookup fails swap(left,right); } template<class T, std::size_t N> BOOST_GPU_ENABLED void swap_impl(T (& left)[N], T (& right)[N]) { for (std::size_t i = 0; i < N; ++i) { ::boost_swap_impl::swap_impl(left[i], right[i]); } } } namespace boost { template<class T1, class T2> BOOST_GPU_ENABLED void swap(T1& left, T2& right) { ::boost_swap_impl::swap_impl(left, right); } } The implementation also

Are there different rules regarding ADL or naming clashes with regard to overloaded operators?

若如初见. 提交于 2019-12-05 21:27:26
I think this example best illustrates my question: namespace N { class C { public: friend bool operator==(const C& c, const C& x) { return true; } friend bool f(const C& c, const C& x) { return true; } }; class D { public: bool operator==(const D& x) { bool a = C{} == C{}; // this works return true; } bool f(const D& x) { bool a = f(C{}, C{}); // this does not work return true; } }; } I have always viewed overloaded operators as being just like function except for the 'calling syntax' if you will. I just noticed the above difference however in ADL or name lookup rules (I don't know which one).

Using equality operators with boost::optional

独自空忆成欢 提交于 2019-12-05 10:49:11
I'm trying to define an equality operator for a type T defined in another namespace, and then use the equality operator on optional<T> . On clang (Apple LLVM 9.1.0), this code: namespace nsp { struct Foo { }; } bool operator==(const nsp::Foo& a, const nsp::Foo& b); void foo() { optional<nsp::Foo> a = none; optional<nsp::Foo> b = none; if (a == b) ; } Results in an error: /usr/local/include/boost/optional/detail/optional_relops.hpp:29:34: error: invalid operands to binary expression ('const nsp::Foo' and 'const nsp::Foo') { return bool(x) && bool(y) ? *x == *y : bool(x) == bool(y); } ~~ ^ ~~

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

♀尐吖头ヾ 提交于 2019-12-05 10:22:46
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 identifier The compiler knows what the constructor's parameter type is, so when I pass X as the argument,

getting an element from a tuple [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-05 09:26:47
问题 This question already has answers here : Closed 8 years ago . 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? 回答1: It's because you attempt to explicitly instantiate get function

Do custom container iterators guarantee ADL to consider namespace std?

让人想犯罪 __ 提交于 2019-12-05 04:18:51
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 considered by ADL? Thanks in advance. I believe that the answer is no in the most general case, but yes

3.4.2 Argument-dependent name lookup from n3290 Draft

僤鯓⒐⒋嵵緔 提交于 2019-12-05 02:25:56
A point from ISO draft n3290 section 3.4.2 paragraph 1: When the postfix-expression in a function call is an unqualified-id , other namespaces not considered during the usual unqualified lookup may be searched, and in those namespaces, namespace-scope friend function declarations not otherwise visible may be found. These modifications to the search depend on the types of the arguments (and for template template arguments, the namespace of the template argument). Here they said aboout "these modifications to the search depend on the types of the arguments / template template arguments /

Lookup of dependent names in C++ template instantiation

风流意气都作罢 提交于 2019-12-04 10:03:18
问题 When I try to compile this code // void foobar(int); template <class T> struct Foo { void bar(T t) { foobar(t); }; }; void foobar(int); template class Foo<int>; with g++ 4.8.2 I get the following error message foo.cc: In instantiation of ‘void Foo<T>::bar(T) [with T = int]’: foo.cc:10:16: required from here foo.cc:5:27: error: ‘foobar’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] void bar(T t) { foobar