🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3321

Re: Observations on templates

#3321
From: Fergus Henderson
Date: Mon, 12 Feb 1996 18:22
46 lines
2351 bytes
vandevod@cs.rpi.edu (David Vandevoorde) writes:

>>>>>> "FH" == Fergus Henderson <fjh@munta.cs.mu.OZ.AU> writes:
>[...]
>FH> template <class T, class S> T cast_d(S &s) 	
>	{ return dynamic_cast<T>s; }
>FH> template <class T, class S> T cast_d(const S &s) 
>	{ return dynamic_cast<T>s; }
>FH> template <class T, class S> T cast_d(volatile S &s)
>FH>	{ return dynamic_cast<T>s; }
>FH> template <class T, class S> T cast_d(const volatile S &s)
>FH> 	{ return dynamic_cast<T>s; }
>
>Isn't the last one sufficient?

No, because 5.2.6[expr.cast.dynamic]/1 states that "dynamic_cast shall
not cast away constness".  If you only defined the last one, then code
such as

	class Foo : public Bar { /* ... */ };
	void f(Bar& x) {
		cast_d<Foo&>(x);
	}

would be ill-formed, because it would instantiate

	cast_d<Foo&, Bar> Foo& cast_d(const volatile Bar &s)
	{ return dynaic_cast<Foo&>(s); }

and this dynamic_cast would be casting away constness.

Incidentally, 5.2.6 would seem to imply that dynamic_cast *is* allowed
to cast away volatileness, but I think that is a mistake in the current
draft.

>(and aren't parentheses required in `dynamic_cast<T>(s)'?).

Yes, they are -- thanks for correcting that error.

--
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.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4fnp8i$8e2@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net> <4fl2cn$hue@mulga.cs.mu.OZ.AU> <xsog2cgbnjx.fsf@juicer.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Newsgroups: comp.std.c++
Subject: Re: Observations on templates
Date: 12 Feb 1996 10:22:39 PST
Organization: Comp Sci, University of Melbourne
Approved: austern@isolde.mti.sgi.com
Message-ID: <4fnp8i$8e2@mulga.cs.mu.OZ.AU>
References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net>  <4fl2cn$hue@mulga.cs.mu.OZ.AU> <xsog2cgbnjx.fsf@juicer.cs.rpi.edu>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 12 Feb 1996 16:18:25 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMR+FfEy4NqrwXLNJAQHtfQIAmZeoC5ba2pfg4wcFNmZchxIVIBDmTmSC
 VL9X6kCi4CR8M9pWVigk8O1Y5uZmKkwsCO1CVmUXFDOLvJdAEszIuw==
 =ffMG
Originator: austern@isolde.mti.sgi.com