argument-dependent-lookup

Why was argument dependent lookup invented?

本小妞迷上赌 提交于 2019-12-01 03:35:21
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? ADL was invented to allow the Interface Principle: The Interface Principle For a class X, all functions, including free functions, that both "mention" X, and are "supplied with" X are

Is it possible to create a trait to answer if a type comes from std?

烈酒焚心 提交于 2019-12-01 01:34:00
问题 After this question by utilizing ADL one can create a trait to answer if the passed type comes from our namespace: #include <utility> namespace helper { template <typename T, typename = void> struct is_member_of_sample : std::false_type { }; template <typename T> struct is_member_of_sample< T, decltype(adl_is_member_of_sample(std::declval<T>()))> : std::true_type { }; } namespace sample { template <typename T> auto adl_is_member_of_sample(T && ) -> void; } // -- Test it namespace sample {

What's the point of iter_swap?

佐手、 提交于 2019-11-30 16:49:37
I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap , and of course it only uses swap instead of std::swap since we're already in namespace std , anyway. That leads me to the next question: Why would anybody write this: using std::swap; swap(a, b); instead of this? std::iter_swap(&a, &b); // saved an entire line of code! Are there any important differences/issues I am overlooking here? From the SGI docs ( here ): [1] Strictly speaking, iter_swap is redundant.

ADL with typedefs from another namespace

对着背影说爱祢 提交于 2019-11-30 07:59:19
问题 I have something like this: #include <iostream> namespace N { typedef std::pair<int, double> MyPair; std::ostream& operator << (std::ostream& o, MyPair const & mypair) { /// } } int main() { N::MyPair pr; std::cout << pr; } This naturally doesn't work, because ADL won't find operator<< because namespace N is not associated with MyPair (unfortunately). Afaik one may not add to namespace std, so if I chose to define operator << in std that would be kinda illegal. So... what to do in such

what does `using std::swap` inside the body of a class method implementation mean?

拥有回忆 提交于 2019-11-30 07:47:53
问题 I was trying to learn and adopt the copy-swap idiom following this thorough explanation on this question: the Copy-Swap Idiom. But I found some code I had never seen: using std::swap; // allow ADL in this example class dumb_array { public: // ... void swap(dumb_array& pOther) // nothrow { using std::swap; // allow ADL /* <===== THE LINE I DONT UNDERSTAND */ swap(mSize, pOther.mSize); // with the internal members swapped, swap(mArray, pOther.mArray); // *this and pOther are effectively swapped

Different behavior for qualified and unqualified name lookup for template

China☆狼群 提交于 2019-11-30 06:45:52
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 read(info1& x) { std::cout << "overload" << std::endl; } int main() { info1 x; info2 y; call_read(x);

Why will two-phase lookup fail to choose overloaded version of 'swap'?

人走茶凉 提交于 2019-11-30 03:24:49
问题 I am studying this fascinating answer to a subtle question regarding the best practice to implement the swap function for user-defined types . (My question was initially motivated by a discussion of the illegality of adding types to namespace std.) I will not re-print the code snippet from the above-linked answer here. Instead, I would like to understand the answer. The answer I've linked above states, beneath the first code snippet, in regards to overloading swap in namespace std (rather

What's the point of iter_swap?

元气小坏坏 提交于 2019-11-29 16:41:13
问题 I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap , and of course it only uses swap instead of std::swap since we're already in namespace std , anyway. That leads me to the next question: Why would anybody write this: using std::swap; swap(a, b); instead of this? std::iter_swap(&a, &b); // saved an entire line of code! Are there any important differences

C++11 style SFINAE and function visibility on template instantiation

≡放荡痞女 提交于 2019-11-29 16:00:11
I'm not sure if this has anything to do with sfinae, or just something thats relevant for any templated function. I am attempting to use sfinae to enable/disable a member function based on existence of corresponding free function, which in turn is enabled/disabled based on existance of member function in another type, all using method described here : struct S; template <typename T> inline auto f(S& s, T const& t) -> decltype(t.f(s), void()) { t.f(s); } struct S { template <typename T> auto f(T const& t) -> decltype(f(*this, t), void()) { f(*this, t); // <--------------------------------------

what does `using std::swap` inside the body of a class method implementation mean?

筅森魡賤 提交于 2019-11-29 09:04:57
I was trying to learn and adopt the copy-swap idiom following this thorough explanation on this question: the Copy-Swap Idiom . But I found some code I had never seen: using std::swap; // allow ADL in this example class dumb_array { public: // ... void swap(dumb_array& pOther) // nothrow { using std::swap; // allow ADL /* <===== THE LINE I DONT UNDERSTAND */ swap(mSize, pOther.mSize); // with the internal members swapped, swap(mArray, pOther.mArray); // *this and pOther are effectively swapped } }; what does using std::swap; mean inside the body of a function implementation ? what does ADL