Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

C++11: non-member begin() and end()

, 8 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
One of the addition to the C++ 2011 standard that is perhaps not so much popularized is the non-member begin() and end(). In STL all containers have a non-static member begin() and end() methods that return an iterator to the … Continue reading →

One of the addition to the C++ 2011 standard that is perhaps not so much popularized is the non-member begin() and end(). In STL all containers have a non-static member begin() and end() methods that return an iterator to the beginning and the end of the container. Therefor iterating over a container could look like this:

std::vector<int> v;
for(auto it = v.begin(); it != v.end(); ++it)
    std::cout << *it << std::endl;

The problem here is that not all user-defined containers have begin() and end(), which makes them impossible to use with the STL algorithms or any other user-defined template function that requires iterators. That is even more problematic when using C arrays. For instance, using a C array with a standard algorithm is quite different than using a vector, for instance.

int inc(n) {return n+1;}

int a[] = {1,2,3,4,5};
std::transform(&a[0], &a[0] + sizeof(a)/sizeof(a[0]), &a[0], inc);

std::vector<int> v(&a[0], &a[0] + sizeof(a)/sizeof(a[0]));
std::transform(v.begin(), v.end(), v.begin(), inc);

The non-member begin() and end() methods are extensible, in the sense they can be overloaded for any type (including C arrays). Herb Sutter argues in his Elements of Modern C++ Style article that you should always prefer the non-member version. They promote uniformity and consistency and allow for more generic programming.

Always use nonmember begin(x) and end(x) (not x.begin() and x.end()), because begin(x) and end(x) are extensible and can be adapted to work with all container types – even arrays – not just containers that follow the STL style of providing x.begin() and x.end() member functions.

The code above can now look like this:

std::vector<int> v {1,2,3,4,5};
for(auto it = begin(v); it != end(v); ++it)
    std::cout << *it << std::endl;

std::transform(begin(v), end(v), begin(v), [](int n){return n+1;});

As for the C array we can overload begin() and end() to look maybe like this:

template <typename T, size_t size>
T* begin(T (&c)[size])
{
    return &c[0];
}

template <typename T, size_t size>
T* end(T (&c)[size])
{
    return &c[0] + size;
}

With this available we can write:

int a[] = {1,2,3,4,5};

std::transform(begin(a), end(a), begin(a), [](int n) {return n+1;});

for(auto it = begin(a); it != end(a); ++it)
    std::cout << *it << std::endl;

If you’d argue that non-member begin() and end() break the encapsulation then I suggest you read Scott Meyers’ How Non-Member Functions Improve Encapsulation where he explains the opposite.

If you’re writing a function that can be implemented as either a member or as a non-friend non-member, you should prefer to implement it as a non-member function. That decision increases class encapsulation. When you think encapsulation, you should think non-member functions.

There is still one question left: what about the other container functions that return iterators, rbegin(), rend(), cbegin(), cend(), crbegin(), and crend()? Currently they are not implemented as non-member functions. According to Herb Sutter, the omission of cbegin() and cend() was an oversight and it will be fixed.

Additional readings:

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Marius Bancila
Software Developer (Senior) Visma Software
Romania Romania
Marius Bancila is a Microsoft MVP for VC++. He works as a software developer for Visma, a Norwegian-based company. He is mainly focused on building desktop applications with VC++ and VC#. He keeps a blog at http://www.mariusbancila.ro/blog, focused on Windows programming. He is the co-founder of codexpert.ro, a community for Romanian C++ programmers.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberosy7-Oct-12 22:09 
GeneralRe: My vote of 5 PinmemberMarius Bancila7-Oct-12 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 8 Oct 2012
Article Copyright 2012 by Marius Bancila
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid