|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-07-19 14:49:56
From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> > No, whether the template parameter is const or not doesn't matter. In
> >
> > shared_ptr<T const> pt(new T);
> >
> > the type that pt has been initialized with is 'T', not 'T const', just
> like
> > in
> >
> > shared_ptr<T const> pt2(new Y);
> >
> > 'pt2' is initialized with 'Y'.
> >
> I don't understand this.
> What do you mean with "is initialized with T and not T const"?
The actual pointer type that is passed to the constructor is 'T *' (because
this is what 'new T' returns.)
> Consider: (I tested this with 1.28.0)
>
> struct A { void foo() ; void bar() const ; } ;
>
> shared_ptr<A> pa ( new A ) ;
> shared_ptr<A const> pca ( new A ) ;
>
> pa->foo() ; // OK
> pa->bar() ; // OK
>
> pca->foo() ; // non-const methods calls for constant object
Yes, because operator-> returns 'A const *'.
> > The only place where the 'counted_base const *' problem appears in
> practice
> > (AFAIK) is:
> >
> > class X: public counted_base // allow this -> shared_ptr conversions
> > {
> > public:
> >
> > shared_ptr<X> shared_this()
> > {
> > return shared_ptr<X>(this); // OK
> > }
> >
> > shared_ptr<X const> shared_this() const
> > {
> > return shared_ptr<X const>(this); // should be a compile-time
> error
>
> It doesn't produce a compile-time error currently. Why should it?
It does for me... the constructor
template<class P, class D> shared_count::shared_count(P, D, counted_base
const *) is private (and never defined.)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk