senjuhashirama / pugixml

Automatically exported from code.google.com/p/pugixml
0 stars 0 forks source link

True const_node_iteratorS #147

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Example with boost::filter_iterator:

pugi::xml_node node;
boost::make_filter_iterator([](const pugi::xml_node&) { return true; }, 
node.begin(), node.end());

What is the expected output? What do you see instead?

I would expect that to work and filter the iterators with the given predicate.
Instead this issues a compiler error.

Which version of pugixml are you using? On what operating system/compiler?

pugixml 1.0 ArchLinux 

$ uname -a
Linux matisse 3.2.6-1-ARCH #1 SMP PREEMPT Tue Feb 14 09:11:26 CET 2012 x86_64 
Intel(R) Xeon(R) CPU E5430 @ 2.66GHz GenuineIntel GNU/Linux
$ gcc --version
gcc (GCC) 4.6.2 20120120 (prerelease)

Please provide any additional information below.

This results in an error because xml_node_iterator::operator* is not const. A 
fix would be to add a proper const_iterator and iterator classes.

Original issue reported on code.google.com by bootsare...@gmail.com on 16 Feb 2012 at 1:37

GoogleCodeExporter commented 9 years ago
This is a huge oversight :(

Adding const iterators won't solve this issue - node.begin() for non-const node 
would still return non-const iterator with the same issue.

The problem is that operator* in the iterator is non-const; the reason for it 
is that it can't return a const-reference to xml_node, because that would make 
calling non-const methods, such as remove, impossible. I can return it by 
value, but there's no fix for operator-> here.

What I can do is I can add const versions of operator*/operator-> to the 
existing iterators - this should not break the existing code (since they did 
not work anyway), and should fix the problem. 

Original comment by arseny.k...@gmail.com on 16 Feb 2012 at 5:48

GoogleCodeExporter commented 9 years ago
It's a little confusing. `xml_node::begin` is const but does not return a true 
const_iterator. The issue with `container::begin` returning a normal iterator 
is a known defect. Usually the declaration of the node has to be changed to 
`const xml_node` to work around. Alternatively, there are the new `cbegin` and 
`cend`. That said, I would like to see support for global `begin`/`end` as I 
prefer them over the members.

Original comment by bootsare...@gmail.com on 16 Feb 2012 at 8:03

GoogleCodeExporter commented 9 years ago
Should be fixed in r858 - works with boost::make_filter_iterator from Boost 
1.48.

Original comment by arseny.k...@gmail.com on 14 Mar 2012 at 5:41