Thread View: comp.std.c++
5 messages
5 total messages
Started by Philippe Verdy
Wed, 27 Mar 1996 21:41
Re: double const declarations
Author: Philippe Verdy
Date: Wed, 27 Mar 1996 21:41
Date: Wed, 27 Mar 1996 21:41
56 lines
2456 bytes
2456 bytes
dak <pierreba@poster.cae.ca> s'icrit :
> I've just encountered a problem in one of our template design and wondered
> what could be done about it. It concerns const reference return value from
> a templated member function, for example:
>
> template <class T> class SmartPtr
> {
> // ... whatever:
> public:
> const T & Dereference ();
> };
>
> The problem arise when T is instantiated with a Container of constants:
>
> SmartPtr<const int> a;
>
> Here we have a double const which is not supported by the compiler, nor the
> standard, I believe. Is there a fix or should we abandon all hope of using
> const returns in templates ?
Declare a temporary template<class T> to use <T> types only, but no <const T>,
then create another template class that will use <const T> as the parameter
of the first template.
To make this class usable for const and non-const references, you can add
another template parameter to the first class : one for the case T should be
const, one for the other case.
Example:
template <class T, class CT>
class SmartPtr2 {
SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
CT & Dereference() {
return *mp ;
}
private :
CT *mp ;
} ;
template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
Now you can safely use:
FreeSmartPtr<const X> and ConstSmartPtr<X>.
You cannot use:
ConstSmartPtr<const X>
due to the double const problem;
But you can use:
FreeSmartPtr<X>
no const at all !
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: double const declarations
Author: Fergus Henderson
Date: Thu, 28 Mar 1996 15:56
Date: Thu, 28 Mar 1996 15:56
30 lines
1633 bytes
1633 bytes
Philippe Verdy <100105.3120@compuserve.com> writes:
>template <class T, class CT>
>class SmartPtr2 {
> SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
> CT & Dereference() {
> return *mp ;
> }
> private :
> CT *mp ;
>} ;
>template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
>template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
You forgot to delegate the constructors.
(Of course, if C++ supported tempate typedefs, there wouldn't be
any need to delegate constructors in examples like this.
Unfortunately it doesn't.)
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: double const declarations
Author: James Christodouleas
Date: Sat, 30 Mar 1996 06:03
Date: Sat, 30 Mar 1996 06:03
37 lines
1969 bytes
1969 bytes
In article <4jdvnj$o35@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus
Henderson) wrote:
> Philippe Verdy <100105.3120@compuserve.com> writes:
>
> >template <class T, class CT>
> >class SmartPtr2 {
> > SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
> > CT & Dereference() {
> > return *mp ;
> > }
> > private :
> > CT *mp ;
> >} ;
> >template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
> >template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
>
> You forgot to delegate the constructors.
>
> (Of course, if C++ supported tempate typedefs, there wouldn't be
> any need to delegate constructors in examples like this.
> Unfortunately it doesn't.)
What does it mean to "delegate constructors?" I looked it up in Coplien,
Advanced C++, and he talks about delegation to mean different classes
sharing functionality dynamically.
What do you mean here?
Jim Christodouleas
jchristo@mit.edu
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: double const declarations
Author: Tony Cook
Date: Mon, 01 Apr 1996 04:13
Date: Mon, 01 Apr 1996 04:13
49 lines
2674 bytes
2674 bytes
James Christodouleas (jchristo@mit.edu) wrote:
: In article <4jdvnj$o35@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus
: Henderson) wrote:
: > Philippe Verdy <100105.3120@compuserve.com> writes:
: >
: > >template <class T, class CT>
: > >class SmartPtr2 {
: > > SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
: > > CT & Dereference() {
: > > return *mp ;
: > > }
: > > private :
: > > CT *mp ;
: > >} ;
: > >template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
: > >template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
: >
: > You forgot to delegate the constructors.
: >
: > (Of course, if C++ supported tempate typedefs, there wouldn't be
: > any need to delegate constructors in examples like this.
: > Unfortunately it doesn't.)
: What does it mean to "delegate constructors?" I looked it up in Coplien,
: Advanced C++, and he talks about delegation to mean different classes
: sharing functionality dynamically.
He means that the new classes, ConstSmartPtr and FreeSmartPtr do not
have constructors, and cannot be created, so constructors that
simply pass on the parameters are required (delegation).
There's another problem anyway - Phillippe used private inheritence,
so a user cannot access the Dereference member function anyway.
[Moderator's note: and `SmartPtr' should be `SmartPtr2',
and `const_cast<CT>' should be `const_cast<CT*>'. This is
why it is a good idea to try compiling your examples
before posting them ;-). -fjh. ]
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: double const declarations
Author: Fergus Henderson
Date: Mon, 01 Apr 1996 18:42
Date: Mon, 01 Apr 1996 18:42
64 lines
3072 bytes
3072 bytes
jchristo@mit.edu (James Christodouleas) writes:
>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
>
>> Philippe Verdy <100105.3120@compuserve.com> writes:
>>
>> >template <class T, class CT>
>> >class SmartPtr2 {
>> > SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
>> > CT & Dereference() {
>> > return *mp ;
>> > }
>> > private :
>> > CT *mp ;
>> >} ;
>> >template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
>> >template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
>>
>> You forgot to delegate the constructors.
[...]
>What does it mean to "delegate constructors?"
Delegation is when the code to implement an method (in this case,
the constructor) in one class just "delegates" the work to someone
else by calling the same method in another class.
Perhaps the simplest way to explain what I meant is to write out the code:
template <class T> class ConstSmartPtr : SmartPtr2<T, T> {
// delegate the constructor
CountSmartPtr(T* p) : SmartPtr2(p) {}
};
template <class T> class FreeSmartPtr : SmartPtr2<T, const T> {} ;
// delegate the constructor
FreeSmartPtr(T* p) : SmartPtr2(p) {}
};
In C++, most operations are automatically delegated from a derived class
to its base class -- this is called inheritence. However, C++ doesn't
provide any special support for delegating constructors. That has to be
done manually.
I also wrote:
>> (Of course, if C++ supported template typedefs, there wouldn't be
>> any need to delegate constructors in examples like this.
>> Unfortunately it doesn't.)
In case that wasn't clear, I meant that instead of the above code, if
C++ supported template typedefs, you could write
template <class T> typedef SmartPtr2<T, T> ConstSmartPtr;
template <class T> typedef SmartPtr2<T, const T> FreeSmartPtr;
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads