Topic: double const declarations
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/04/01 Raw View
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
]
Author: Philippe Verdy <100105.3120@compuserve.com>
Date: 1996/03/27 Raw View
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 ]
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/03/28 Raw View
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 ]
Author: David Byrden <Goyra@iol.ie>
Date: 1996/03/28 Raw View
dak <pierreba@poster.cae.ca> wrote:
>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 ?
Sheer common sense tell you that we have to allow this. It can arise
with 'volatile' as well. The Microsoft compiler gives just a warning.
David
[ 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 ]
Author: jchristo@mit.edu (James Christodouleas)
Date: 1996/03/30 Raw View
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 ]
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1996/04/01 Raw View
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 ]
Author: dak <pierreba@poster.cae.ca>
Date: 1996/03/25 Raw View
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 ?
---
[ 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
]
Author: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
Date: 1996/03/26 Raw View
In article <Pine.A32.3.91.960325093715.29874A-100000@zorglub.cae.ca>
dak <pierreba@poster.cae.ca> writes:
|> 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 ?
The draft standard forbids two explicit const's. If the duplicate
Const result from typedef's, etc., it's OK. (I don't know if the
actual words mention template instantiation types, but if they don't
I'm sure that it is an editorial oversight, and not the intent.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, tudes et r alisations en logiciel orient objet --
-- A la recherche d'une activit dans une region francophone
[ 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 ]