In a recent interview, Herb Sutter recommended choosing free begin(container) end(container) function templates over container.begin() . I like it because this function can be provided for all repeating types that don't come with begin () / end () methods. Since most of my domain classes have interfaces that speak the domain language and do not use common names such as start / end, now I can provide an iterative interface compatible with STL containers and range bases for loops without spoiling the interface of the main class. I am wondering what is the best way to provide begin / end functions for my own types. My first thought was to do it the same way as with swap , and write a function in the same namespace where my type lives.
namespace My { class Book { public: typedef std::vector<Page>::const_iterator PageIterator; PageIterator FirstPage() const { return begin(pages_); } PageIterator LastPage() const { return end(pages_); } private: std::vector<Page> pages_; }; Book::PageIterator begin(const Book& b) { return b.FirstPage(); } Book::PageIterator end(const Book& b) { return b.LastPage(); } }
Can you rely on ADL here, or should they be in the std namespace? I think the other way is to provide specialization in the std namespace (overloading in std is not allowed, right?). What is the best way espacially regarding loop-based range searching?
c ++ c ++ 11
hansmaad
source share