🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Observations on templates

#3319
From: Fergus Henderson
Date: Mon, 12 Feb 1996 05:03
26 lines
1718 bytes
solution@gate.net (Ken Walter) writes:

>I always thought the following was interesting:
>
>template <class T, class S> T cast_d( S s) { return dynamic_cast<T>s; }

That won't quite do what you want, because you are using
pass-by-value.  That will result in copying, giving `cast_d' quite
different semantics to `dynamic_cast'.  You should use
pass-by-reference instead.  You then need to overload the function to
allow all possible sorts of references:

template <class T, class S> T cast_d(S &s) 	 { return dynamic_cast<T>s; }
template <class T, class S> T cast_d(const S &s) { return dynamic_cast<T>s; }
template <class T, class S> T cast_d(volatile S &s)
						 { return dynamic_cast<T>s; }
template <class T, class S> T cast_d(const volatile S &s)
						 { return dynamic_cast<T>s; }

--
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.  Moderation policy:
  http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4fl2cn$hue@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net>

Show Full Headers
Complete Article Headers
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Message-ID: <4fl2cn$hue@mulga.cs.mu.OZ.AU>
X-Original-Date: 11 Feb 1996 15:35:50 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 12 Feb 96 05:03:19 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: Observations on templates
Organization: Comp Sci, University of Melbourne
References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net>
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMR7KOuEDnX0m9pzZAQGoWgGAlZHAIcIBefVaSTeQhykePENU9Aa4a4cf
 Jvqfc2rAkls7rKu02UJyRAN7g+or4wHb
 =ILLW