The established idiom for invoking swap is:
using std::swap
swap(foo, bar);
This way, swap can be overloaded for user-defined types outside of the std namespace.
Should we invoke begin and end in the same fashion?
using std::begin;
using std::end;
some_algorithm(begin(some_container), end(some_container));
Or should we just write:
some_algorithm(std::begin(some_container), std::end(some_container));
Using a
using-declaration like that is the correct way IMO. It's also what the standard does with the range for loop: if there is nobeginorendmembers present then it will callbegin(x)andend(x)withstdas an associated namespace (i.e. it will findstd::beginandstd::endif ADL doesn't find non-memberbeginandend).If you find that writing
using std::begin; using std::end;all the time is tedious then you can use theadl_beginandadl_endfunctions below:This code is pretty monstrous. Hopefully with C++14 this can become less arcane: