When creating a custom container class that plays by the usual rules (i.e. works with STL algorithms, works with well-behaved generic code, etc.), in C++03 it was sufficient to implement iterator support and member begin/end functions.
C++11 introduces two new concepts - range-based for loop and std::begin/end. Range-based for loop understands member begin/end functions, so any C++03 containers support range-based for out of the box. For algorithms the recommended way (according to 'Writing modern C++ code' by Herb Sutter) is to use std::begin instead of member function.
However, at this point I have to ask - is the recommended way to call a fully qualified begin() function (i.e. std::begin(c)) or to rely on ADL and call begin(c)?
ADL seems useless in this particular case - since std::begin(c) delegates to c.begin() if possible, usual ADL benefits do not seem to apply. And if everybody starts to rely on ADL, all custom containers have to implement extra begin()/end() free functions in their requisite namespaces. However, several sources seem to imply that unqualified calls to begin/end are the recommended way (i.e. https://svn.boost.org/trac/boost/ticket/6357).
So what is the C++11 way? Should container library authors write extra begin/end functions for their classes to support unqualified begin/end calls in absence of using namespace std; or using std::begin;?
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()
andend()
function templates inside alegacy
namespace to retrofit the required functionality onto any class or class template that can provide it, but has e.g. the wrong naming conventions. Calling code can then rely on ADL to find these new functions. Example code (based on comments by @Xeo):Pros: consistent and terse calling convention that works completely generically
.begin()
and.end()
legacy::Container<T>
that does not have member.begin()
andend()
without requiring source code modificationsCons: requires using-declarations in many places
std::begin
andstd::end
are required to have been brought into every explicit calling scope as fall back options for C-style arrays (potential pitfall for template headers and general nuisance)ADL through custom non-member
adl_begin()
andadl_end()
A second alternative is to encapsulate the using-declarations of the previous solution into a separate
adl
namespace by providing non-member function templatesadl_begin()
andadl_end()
, which can then also be found through ADL. Example code (based on comments by @Yakk):Pros: consistent calling convention that works completely generically
Cons: a little verbose
adl_begin()
/adl_end()
is not as terse asbegin()
/end()
std::begin
/std::end
NOTE: Not sure if this really improves upon the previous approach.
Explicitly qualifying
std::begin()
orstd::end()
everywhereOnce the verbosity of
begin()
/end()
has been given up anyway, why not go back to the qualified calls ofstd::begin()
/std::end()
? Example code:Pros: consistent calling convention that works almost generically
.begin()
and.end()
Cons: a little verbose and retrofitting is not generic and a maintainence problem
std::begin()
/std::end()
is a little more verbose thanbegin()
/end()
LegacyContainer
that does not have member.begin()
andend()
(and for which there is no source code!) by providing explicit specializations of the non-member function templatesbegin()
andend()
innamespace std
LegacyContainer<T>
by directly adding member functionsbegin()
/end()
inside the source code ofLegacyContainer<T>
(which for templates is available). Thenamespace std
trick does not work here because function templates cannot be partially specialized.What to use?
The ADL approach through non-member
begin()
/end()
in a a container's own namespace is the idiomatic C++11 approach, especially for generic functions that require retrofitting on legacy classes and class templates. It is the same idiom as for user-providing non-memberswap()
functions.For code that only uses Standard Containers or C-style arrays,
std::begin()
andstd::end()
could be called everywhere without introducing using-declarations, at the expense of more verbose calls. This approach can even be retrofitted but it requires fiddling withnamespace std
(for class types) or in-place source modifcations (for class templates). It can be done, but is not worth the maintainence trouble.In non-generic code, where the container in question is known at coding-time, one could even rely on ADL for Standard Containers only, and explicitly qualify
std::begin
/std::end
for C-style arrays. It loses some calling consistency but saves on using-declarations.