Boost logo

Boost Users :

From: François Duranleau (duranlef_at_[hidden])
Date: 2005-10-27 12:09:12


On Wed, 26 Oct 2005, Joshua Schpok wrote:

> I want to make a 3D array of std::lists:
>
> boost::multi_array<std::list<CSomething>,3> listbin;
>
> Then I'd like to actually use those lists:
>
> /// Walk through list at x,y,z
> for(std::list<CSomething>::iterator iter = listbin[x][y][z].begin();
> reti != iter[x][y][z].end(); iter++)
> {
> iter->m_foo += 42;
> }
>
> But multi_array is giving me back *const* list iterators, producing the
> error:
>
> error: conversion from `std::_List_const_iterator<CSomething>' to
> non-scalar type `std::_List_iterator<CSomething>'
> requested
>
> Why is this so? I can trick the compiler by doing a cast like:

Seems like your variable listbin is const itself (or a const reference).
Boost.MultiArray's operator[] is overloaded for const, so if your listbin
is not const, it should not be a problem.

> std::list<CSomething>::iterator iter =
> ((std::list<CSomething>)listbin[x][y][z]).begin();
>
> but this seems to poop out at runtime.

Of course, you are making a copy of your list through this cast! And then
you take an iterator to a temporary object. When you dereference that
iterator: boom!

> Is there a proper way to do this?

Make sure listbin is not const!

--
Francois

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net