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 nobegin
orend
members present then it will callbegin(x)
andend(x)
withstd
as an associated namespace (i.e. it will findstd::begin
andstd::end
if ADL doesn't find non-memberbegin
andend
).If you find that writing
using std::begin; using std::end;
all the time is tedious then you can use theadl_begin
andadl_end
functions below:This code is pretty monstrous. Hopefully with C++14 this can become less arcane: