[foreach] something strange is happening ...

Hi Boosters! I've been playing around with BOOST_FOREACH a little and got some unexpected results. The following program crashes when I try to iterate a const map with BOOST_FOREACH. That's really strange because for the non-const map everything works fine. Please take a look at the source. What's wrong here? Did I miss something? This is the version in cvs on vc-8_0. Cheers Sascha ----------------------------------------------------------------- <source> #include <boost/foreach.hpp> #include <map> class foo { public: typedef std::map<int, float> map_type; /// return copy of map map_type map() const { return _map; } /// return const copy of map map_type const map_c() const { return _map; } private: map_type _map; }; void main() { foo f; // iterate copy ... ok BOOST_FOREACH( foo::map_type::value_type i, f.map() ) {} // iterate const copy ... crashes here! :-( BOOST_FOREACH( foo::map_type::value_type i, f.map_c() ) {} } </source>

Oh, I had a look at the regression results and found this problem to be an expected failure on vc-8_0. A regression note says: "This compiler does not support detection of const rvalues" hm... i wonder why I didn't get a compile error or at least some warning? however, sorry for the noise and keep up the good work! sascha Sascha Seewald wrote:
Hi Boosters!
I've been playing around with BOOST_FOREACH a little and got some unexpected results. The following program crashes when I try to iterate a const map with BOOST_FOREACH. That's really strange because for the non-const map everything works fine.
Please take a look at the source. What's wrong here? Did I miss something?
This is the version in cvs on vc-8_0.
Cheers Sascha
----------------------------------------------------------------- <source>
#include <boost/foreach.hpp> #include <map>
class foo { public: typedef std::map<int, float> map_type;
/// return copy of map map_type map() const { return _map; }
/// return const copy of map map_type const map_c() const { return _map; }
private: map_type _map; };
void main() { foo f;
// iterate copy ... ok BOOST_FOREACH( foo::map_type::value_type i, f.map() ) {}
// iterate const copy ... crashes here! :-( BOOST_FOREACH( foo::map_type::value_type i, f.map_c() ) {} }
</source>
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Sascha Seewald wrote:
Oh, I had a look at the regression results and found this problem to be an expected failure on vc-8_0.
A regression note says: "This compiler does not support detection of const rvalues"
hm... i wonder why I didn't get a compile error or at least some warning?
If I could detect the problem to issue a warning, I could detect const rvalues and just make the code work. ;-) -- Eric Niebler Boost Consulting www.boost-consulting.com

Sascha Seewald wrote:
Hi Boosters!
I've been playing around with BOOST_FOREACH a little and got some unexpected results. The following program crashes when I try to iterate a const map with BOOST_FOREACH. That's really strange because for the non-const map everything works fine.
Please take a look at the source. What's wrong here? Did I miss something?
You haven't missed anything, and what's wrong here is your compiler, sadly. No version of MSVC is compliant enough to detect const-qualified rvalues. My only advice to you is: don't do that. -- Eric Niebler Boost Consulting www.boost-consulting.com
participants (2)
-
Eric Niebler
-
Sascha Seewald