wangxiaowei0303 / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

C++11 range-based for-loop support #52

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In C++11, one can iterate a collection using the following feature:

    std::list<int> stuff;
    for (const auto& element : stuff) {
      ...
    }

This requires the collection to implement two methods: begin() and end().
rapidjson implements Begin() and End() instead, thanks to the coding rules, I 
suppose.
Adding these methods would change this:

    for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
        printf("%d ", itr->GetInt());

to this:

    for (const auto& elem : a)
        printf("%d ", elem.GetInt());

Would it be possible to add the following methods so that range-based for-loop 
works?

    ValueIterator begin() { return Begin(); }
    ValueIterator end() { return End(); }
    ConstValueIterator begin() const { return Begin(); }
    ConstValueIterator end() const { return End(); }

Original issue reported on code.google.com by hugo.pei...@gmail.com on 18 Jan 2013 at 12:00

GoogleCodeExporter commented 9 years ago

Original comment by milo...@gmail.com on 21 Jan 2013 at 1:15

GoogleCodeExporter commented 9 years ago
I dont think polluting interface is a good idea. One can specialize 
std::begin(std::end) or define free function begin(end) in the corresponding 
namespace (ADL will do the rest).

Original comment by IvochkinSN@gmail.com on 18 Jun 2013 at 1:43