Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constness of the container of shared_ptr and the constness of it elements

Reply
Thread Tools

Constness of the container of shared_ptr and the constness of it elements

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      04-02-2006
In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?

#include <boost/shared_ptr.hpp>
#include <vector>
#include <iterator>
#include <iostream>

class trial {
public:
void const_fun() const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void non_const_fun() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};

int main(){
std::vector<boost::shared_ptr<trial> > v;
v.push_back(boost::shared_ptr<trial>(new trial));
{
std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
(*it)->const_fun();
(*it)->non_const_fun();
}
{
std::vector<boost::shared_ptr<trial> >::const_iterator it =
v.begin();
(*it)->const_fun();
(*it)->non_const_fun();//want a const trial, this function should
not be called.
}
{
std::vector<boost::shared_ptr<const trial> >::iterator it =
v.begin();//compile error
(*it)->const_fun();
(*it)->non_const_fun();
}

std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
//is there any conversion of
this kind?

return EXIT_SUCCESS;
}

 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      04-02-2006
On 2 Apr 2006 13:59:58 -0700, ""
<> wrote:
>In the following program, I want an iterator contain pointer pointing
>to constant object not const pointer. If it is possible would you
>please let me know how to do it?


No. Standard containers are for values only, not for pointers (neither
real nor "smart").

Best wishes,
Roland Pibinger
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-02-2006
Roland Pibinger wrote:
> On 2 Apr 2006 13:59:58 -0700, ""
> <> wrote:
>
>>In the following program, I want an iterator contain pointer pointing
>>to constant object not const pointer. If it is possible would you
>>please let me know how to do it?

>
>
> No. Standard containers are for values only, not for pointers (neither
> real nor "smart").
>

Care to elaborate? Containers are often used for both pointers and
smart pointers.

--
Ian Collins.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      04-03-2006
In article <. com>,
"" <> wrote:

> In the following program, I want an iterator contain pointer pointing
> to constant object not const pointer. If it is possible would you
> please let me know how to do it?


You have to wrap the vector in a class of your own that prohibits such
use.

> #include <boost/shared_ptr.hpp>
> #include <vector>
> #include <iterator>
> #include <iostream>
>
> class trial {
> public:
> void const_fun() const {
> std::cout << __PRETTY_FUNCTION__ << std::endl;
> }
> void non_const_fun() {
> std::cout << __PRETTY_FUNCTION__ << std::endl;
> }
> };
>
> int main(){
> std::vector<boost::shared_ptr<trial> > v;
> v.push_back(boost::shared_ptr<trial>(new trial));
> {
> std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
> (*it)->const_fun();
> (*it)->non_const_fun();
> }
> {
> std::vector<boost::shared_ptr<trial> >::const_iterator it =
> v.begin();
> (*it)->const_fun();
> (*it)->non_const_fun();//want a const trial, this function should
> not be called.


That function should be called. The const_iterator means that the
pointer value itself is const, not the trial object it points to. IE you
can change the trial object, but you can't replace it with a different
trial object.

> }
> {
> std::vector<boost::shared_ptr<const trial> >::iterator it =
> v.begin();//compile error
> (*it)->const_fun();
> (*it)->non_const_fun();
> }
>
> std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
> //is there any conversion of
> this kind?


No, nor would there be if you were using bald pointers.

> return EXIT_SUCCESS;
> }




--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      04-03-2006

Daniel T. wrote:
> In article <. com>,
> "" <> wrote:
>
> > In the following program, I want an iterator contain pointer pointing
> > to constant object not const pointer. If it is possible would you
> > please let me know how to do it?

>
> You have to wrap the vector in a class of your own that prohibits such
> use.
>
> > #include <boost/shared_ptr.hpp>
> > #include <vector>
> > #include <iterator>
> > #include <iostream>
> >
> > class trial {
> > public:
> > void const_fun() const {
> > std::cout << __PRETTY_FUNCTION__ << std::endl;
> > }
> > void non_const_fun() {
> > std::cout << __PRETTY_FUNCTION__ << std::endl;
> > }
> > };
> >
> > int main(){
> > std::vector<boost::shared_ptr<trial> > v;
> > v.push_back(boost::shared_ptr<trial>(new trial));
> > {
> > std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
> > (*it)->const_fun();
> > (*it)->non_const_fun();
> > }
> > {
> > std::vector<boost::shared_ptr<trial> >::const_iterator it =
> > v.begin();
> > (*it)->const_fun();
> > (*it)->non_const_fun();//want a const trial, this function should
> > not be called.

>
> That function should be called. The const_iterator means that the
> pointer value itself is const, not the trial object it points to. IE you
> can change the trial object, but you can't replace it with a different
> trial object.
>
> > }
> > {
> > std::vector<boost::shared_ptr<const trial> >::iterator it =
> > v.begin();//compile error
> > (*it)->const_fun();
> > (*it)->non_const_fun();
> > }
> >
> > std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
> > //is there any conversion of
> > this kind?

>
> No, nor would there be if you were using bald pointers.


If I have some function which can accept a vector of objects and it
doesn't change these objects, I have to define the function accept a
vector of objects (not const objects), because the answer to the above
question is "NO". Is it a drawback of the STL contain? Could it be
improved?

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      04-03-2006
In article < .com>,
"" <> wrote:

> Daniel T. wrote:
> > In article <. com>,
> > "" <> wrote:
> >
> > > In the following program, I want an iterator contain pointer pointing
> > > to constant object not const pointer. If it is possible would you
> > > please let me know how to do it?

> >
> > You have to wrap the vector in a class of your own that prohibits such
> > use.
> >
> > > #include <boost/shared_ptr.hpp>
> > > #include <vector>
> > > #include <iterator>
> > > #include <iostream>
> > >
> > > class trial {
> > > public:
> > > void const_fun() const {
> > > std::cout << __PRETTY_FUNCTION__ << std::endl;
> > > }
> > > void non_const_fun() {
> > > std::cout << __PRETTY_FUNCTION__ << std::endl;
> > > }
> > > };
> > >
> > > int main(){
> > > std::vector<boost::shared_ptr<trial> > v;
> > > v.push_back(boost::shared_ptr<trial>(new trial));
> > > {
> > > std::vector<boost::shared_ptr<trial> >::iterator it = v.begin();
> > > (*it)->const_fun();
> > > (*it)->non_const_fun();
> > > }
> > > {
> > > std::vector<boost::shared_ptr<trial> >::const_iterator it =
> > > v.begin();
> > > (*it)->const_fun();
> > > (*it)->non_const_fun();//want a const trial, this function should
> > > not be called.

> >
> > That function should be called. The const_iterator means that the
> > pointer value itself is const, not the trial object it points to. IE you
> > can change the trial object, but you can't replace it with a different
> > trial object.
> >
> > > }
> > > {
> > > std::vector<boost::shared_ptr<const trial> >::iterator it =
> > > v.begin();//compile error
> > > (*it)->const_fun();
> > > (*it)->non_const_fun();
> > > }
> > >
> > > std::vector<boost::shared_ptr<const trial> > v_const = v;//error,
> > > //is there any conversion of
> > > this kind?

> >
> > No, nor would there be if you were using bald pointers.

>
> If I have some function which can accept a vector of objects and it
> doesn't change these objects, I have to define the function accept a
> vector of objects (not const objects), because the answer to the above
> question is "NO". Is it a drawback of the STL contain? Could it be
> improved?


It's not a drawback of the STL containers. It is a proper and necessary
requirement. The object that the container is going to be destroying is
the one that is (and should be) const when the container is const. If
the container is holding pointers, then *that* is what is, and should
be, const.



--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-03-2006
Roland Pibinger wrote:
> On 2 Apr 2006 13:59:58 -0700, ""
> <> wrote:
>> In the following program, I want an iterator contain pointer pointing
>> to constant object not const pointer. If it is possible would you
>> please let me know how to do it?

>
> No. Standard containers are for values only, not for pointers (neither
> real nor "smart").
> =


Really, Roland? How would you manage a container of polymorphic objects
(classic case in point, a list or vector of Shapes).
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-03-2006
red floyd wrote:
> Roland Pibinger wrote:
>> On 2 Apr 2006 13:59:58 -0700, ""
>> <> wrote:
>>> In the following program, I want an iterator contain pointer pointing
>>> to constant object not const pointer. If it is possible would you
>>> please let me know how to do it?

>>
>> No. Standard containers are for values only, not for pointers (neither
>> real nor "smart").
>> =

>
> Really, Roland? How would you manage a container of polymorphic objects
> (classic case in point, a list or vector of Shapes).


Follow-up. You can store pointers (raw or smart) in a Standard
container. In fact, that's the only way to maintain a container of
polymorphic objects.

The thing is that if you store raw pointers in a container, then *you*
are responsible for managing the lifetime of those pointers -- on a
recent project, we were using raw pointers (legacy code), and I made a
big deal during design reviews that pointer ownership and lifetime
maintenance had to be specified in the design (we were storing them in
containers).

If you use a smart pointer, then the smart pointer manages the lifetime
of the pointer, and you don't have to deal with it.
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      04-03-2006
On Mon, 03 Apr 2006 10:37:28 +1200, Ian Collins <ian->
wrote:
>Roland Pibinger wrote:
>> No. Standard containers are for values only, not for pointers (neither
>> real nor "smart").
>>

>Care to elaborate? Containers are often used for both pointers and
>smart pointers.


'Value semantics' is one of the central characteristics of STL. All
containers, iterators and algorithms are designed for values only. You
may also use pointers as template arguments but they cause various
problems: clumsiness in usage, no 'const correctness' (see above),
algorithm mismatch (algorithms refer to pointers, not pointed-to
objects), ...
A container for pointers needs a design that is especially made for
pointers. Many library vendors provide (non-Standard) containers for
pointers, see e.g. CTypedPtrArray (MFC), QPtrVector (Qt), ... IMO, the
lack of appropriate containers for pointers is one of the major
reasons for the low acceptance of STL in the real world.

Best wishes,
Roland Pibinger
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      04-03-2006
On Mon, 03 Apr 2006 04:13:15 GMT, red floyd <> wrote:
>Roland Pibinger wrote:
>> No. Standard containers are for values only, not for pointers (neither
>> real nor "smart").
>> =

>Really, Roland?


Yes, really!

>How would you manage a container of polymorphic objects
>(classic case in point, a list or vector of Shapes).


With a container for pointers:
http://www.codeproject.com/vcpp/stl/ptr_vecto.asp

Best wishes,
Roland Pibinger
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57