Quantcast

[Iterator]Can dereference() in iterator_adaptor return a temporary value?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Iterator]Can dereference() in iterator_adaptor return a temporary value?

Remi
Hi, I'd like a const iterator that acts as a proxy by giving access to elements of type A as if they were elements of unrelated type B. In that case I guess the dereference() member function should return a temporary value, right?
Would that be a correct implementation of the idea? Here I just want to access an array of SourceVertexType as if it were an array of VertexType.

        template<class VertexType>
        class ConstVerticesIterator : public boost::iterator_adaptor< ConstVerticesIterator<VertexType>, const SourceVertexType*, const VertexType >
        {
            private:

            friend class boost::iterator_core_access;

            VertexType dereference() const
            {
                const SourceVertexType& source_vertex( *base() );
                return VertexType( source_vertex.x, source_vertex.y );
            }


            public:

            explicit ConstVerticesIterator( const SourceVertexType* vertices )
                :
                boost::iterator_adaptor< ConstVerticesIterator<VertexType>, const SourceVertexType*, const VertexType >( vertices )
            {}
        };


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Iterator]Can dereference() in iterator_adaptor return a temporary value?

Nathan Ridge

> Hi, I'd like a const iterator that acts as a proxy by giving access to
> elements of type A as if they were elements of unrelated type B. In
> that case I guess the dereference() member function should return a
> temporary value, right?
> Would that be a correct implementation of the idea? Here I just want to
> access an array of SourceVertexType as if it were an array of
> VertexType.
>
> template<class VertexType>
> class ConstVerticesIterator : public boost::iterator_adaptor<
> ConstVerticesIterator<VertexType>, const SourceVertexType*, const
> VertexType >

Yes, this is fine. This is what transform_iterator does.
 
However, you should specify the fourth template parameter of
iterator_adaptor (Reference) to be "const VertexType" as well,
otherwise it will default to "const VertexType&" and operator*
will return a reference to the temporary rather than returning
a copy.
 
Regards,
Nate    
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Iterator]Can dereference() in iterator_adaptor return a temporary value?

Remi
Thanks, I added that parameter and it seems to work fine.



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...