C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with Boost iterators

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Nov 01, 2006 10:10 am    Post subject: Problem with Boost iterators Reply with quote



Hi all,

I am running into the compilation error (gcc 4.0.2 Suse Linux) while
trying to compile the following simple program:

#include <boost/iterator/iterator_facade.hpp>

struct Pixel { int _abc;};

class PixelIterator :
public boost::iterator_facade< PixelIterator, Pixel,
boost::random_access_traversal_tag >
{
public:
friend class boost::iterator_core_access;
explicit PixelIterator() : _prPixel() {}
explicit PixelIterator(Pixel * iprPixel) : _prPixel(iprPixel) {}
void increment(){++_prPixel;}
void decrement(){--_prPixel;}
bool equal(const PixelIterator& iOther) const{ return _prPixel ==
iOther._prPixel; }
Pixel& dereference() const{return *_prPixel;}
void advance(int iDist){_prPixel += iDist;}
uint distance_to(const PixelIterator& iOther) const{return
iOther._prPixel - _prPixel;}
private:
Pixel * _prPixel;
};

int main()
{
Pixel p[10];
PixelIterator it(p);
it[1]._abc = 1;
}

The error message is
'class boost::detail::operator_brackets_proxy<PixelIterator>' has
no member named '_abc

The Boost website is currently unavailable to look-up the documentation
but I am pretty match sure that the semantic of operator[] (int dist)
is *(it +dist). And indeed if I replace the last line in main with
(*(it+1))._abc = 1;

the error goes away.

Any help is greatly appreciated.
Mike


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carl Barron
Guest





PostPosted: Fri Nov 03, 2006 10:10 am    Post subject: Re: Problem with Boost iterators Reply with quote



In article <1162348295.249095.231880 (AT) e64g2000cwd (DOT) googlegroups.com>,
<michael.alexeev (AT) gmail (DOT) com> wrote:

Quote:
Hi all,

I am running into the compilation error (gcc 4.0.2 Suse Linux) while
trying to compile the following simple program:

#include <boost/iterator/iterator_facade.hpp

struct Pixel { int _abc;};

class PixelIterator :
public boost::iterator_facade< PixelIterator, Pixel,
boost::random_access_traversal_tag
{
public:
friend class boost::iterator_core_access;
explicit PixelIterator() : _prPixel() {}
explicit PixelIterator(Pixel * iprPixel) : _prPixel(iprPixel) {}
void increment(){++_prPixel;}
void decrement(){--_prPixel;}
bool equal(const PixelIterator& iOther) const{ return _prPixel ==
iOther._prPixel; }
Pixel& dereference() const{return *_prPixel;}
void advance(int iDist){_prPixel += iDist;}
uint distance_to(const PixelIterator& iOther) const{return
iOther._prPixel - _prPixel;}
private:
Pixel * _prPixel;
};

int main()
{
Pixel p[10];
PixelIterator it(p);
it[1]._abc = 1;
}

The error message is
'class boost::detail::operator_brackets_proxy<PixelIterator>' has
no member named '_abc

The Boost website is currently unavailable to look-up the documentation
but I am pretty match sure that the semantic of operator[] (int dist)
is *(it +dist). And indeed if I replace the last line in main with
(*(it+1))._abc = 1;

the error goes away.

Any help is greatly appreciated.
Mike
The docs do state that if the proxy solution is not needed as in

your case your class can define operator [] and it will be used instead
of the operator[] supplied by iterator_facade. So if you add
Pixel & operator [] (int n) {return *(_prPixel+n);} to Pixelterator
it will work since nothing is really destroyed here...

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David Abrahams
Guest





PostPosted: Sun Nov 05, 2006 9:15 am    Post subject: Re: Problem with Boost iterators Reply with quote



Alberto Ganesh Barbati <AlbertoBarbati (AT) libero (DOT) it> writes:

Quote:
shunsuke ha scritto:
michael.alexeev (AT) gmail (DOT) com wrote:

int main()
{
Pixel p[10];
PixelIterator it(p);
it[1]._abc = 1;
}

The error message is
'class boost::detail::operator_brackets_proxy<PixelIterator>' has
no member named '_abc

The Boost website is currently unavailable to look-up the documentation
but I am pretty match sure that the semantic of operator[] (int dist)
is *(it +dist). And indeed if I replace the last line in main with
(*(it+1))._abc = 1;

the error goes away.

The standard requires 'operator[]' to return a type
which is *convertible* to 'Pixel'.


That's correct in C++03, but in the latest draft of the standard the
requirement has changed: the return type of operator[] is required to be
convertible to const Pixel&, not Pixel. The intent is clearly to
disallow the use of the return value to modify the referred object.

No, it's not intended to disallow modification; it's only intended to
not require modifiability. An operator[] that returns Pixel& is
conforming, since Pixel& is convertible to Pixel const&.

Quote:
So
the assignment "it[1].abc = 1;" might compile but is non-portable.

With it being an unknown iterator type, yes.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Mon Nov 06, 2006 3:56 am    Post subject: Re: Problem with Boost iterators Reply with quote

David Abrahams ha scritto:
Quote:
Alberto Ganesh Barbati <AlbertoBarbati (AT) libero (DOT) it> writes:
shunsuke ha scritto:
The standard requires 'operator[]' to return a type
which is *convertible* to 'Pixel'.

That's correct in C++03, but in the latest draft of the standard the
requirement has changed: the return type of operator[] is required to be
convertible to const Pixel&, not Pixel. The intent is clearly to
disallow the use of the return value to modify the referred object.

No, it's not intended to disallow modification; it's only intended to
not require modifiability. An operator[] that returns Pixel& is
conforming, since Pixel& is convertible to Pixel const&.

Oh! I understand. Thanks for correcting me. By the way, why isn't
modifiability required for mutable iterators? That seems quite
reasonable. As I'm pretty sure this case has been considered, there must
be some obvious rationale against it that I'm failing to see. Could you
please enlighten me?

Will there be changes in this area if the "new iterators concepts"
proposal gets accepted?

TIA,

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.