How does “using std::swap” enable Argument-Dependent Lookup (ADL)?

Just wanted to add why this idiom is used at all, which seemed like the spirit of the original question. This idiom is used within many std library classes where swap is implemented. From http://www.cplusplus.com/reference/algorithm/swap/: Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental … Read more

Is it okay to define a totally general swap() function?

unique_ptr<T> requires T* to be a NullablePointer [unique.ptr]p3 NullablePointer requires lvalues of T* to be Swappable [nullablepointer.requirements]p1 Swappable essentially requires using std::swap; swap(x, y); to select an overload for x, y being lvalues of type T* [swappable.requirements]p3 In the last step, your type foo::bar produces an ambiguity and therefore violates the requirements of unique_ptr. libstdc++’s … Read more

C++ compile time counters, revisited

After further investigation, it turns out there exists a minor modification that can be performed to the next() function, which makes the code work properly on clang++ versions above 7.0.0, but makes it stop working for all other clang++ versions. Have a look at the following code, taken from my previous solution. template <int N> … Read more

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

This mechanism is normally used in templated code, i.e. template <typename Value> class Foo. Now the question is which swap to use. std::swap<Value> will work, but it might not be ideal. There’s a good chance that there’s a better overload of swap for type Value, but in which namespace would that be? It’s almost certainly … Read more

How do I write an ADL-enabled trailing return type, or noexcept specification?

I think I would move it into a separate namespace namespace tricks { using std::swap; template <typename T, typename U> void swap(T &t, U &u) noexcept(noexcept(swap(t, u))); } template <typename T> void swap(my_template<T>& x, my_template<T>& y) noexcept(noexcept(tricks::swap(std::declval<T>(), std::declval<T>()))) { using std::swap; swap(x.something_that_is_a_T, y.something_that_is_a_T); } Alternatively you can move the whole code up into tricks and … Read more

Should custom containers have free begin/end functions?

There are several approaches, each with their own pros and cons. Below three approaches with a cost-benefit analysis. ADL through custom non-member begin() / end() The first alternative provides non-member begin() and end() function templates inside a legacy namespace to retrofit the required functionality onto any class or class template that can provide it, but … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)