🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

const and template parameters

#120
From: Stephen Vinoski
Date: Mon, 17 Aug 1992 21:57
48 lines
1875 bytes
Recently I wondered about the use of const for the arguments of class
template member functions and function templates.  For example, given
this function template:

	template<class T>
	void
	tfunc(const T arg) { // ... }

what happens when the type ``int *'' is used for the formal template
parameter ``T''?

Mentally, one might make the parameter substitution and come up with
the following equivalent function:

	void tfunc(const int *arg);

Under this interpretation, the argument appears to be a pointer to
const int.

What actually should happen, though, is that const should apply to
``T'', so that the type parameter substitution results in

	void tfunc(int *const arg);

with the argument actually being a const pointer to int.

The latter interpretation is exactly the same as the "const applied to
typedef" problem:

	typedef int *IntPtr;
	const IntPtr int_ptr;

Here, int_ptr is a const pointer to int (sometimes much to the
amazement of C++ novices :-)).

It appears that the use of const under these circumstances is somewhat
misleading, and perhaps buys us very little.  Should developers leave
const out of these areas of function and class templates?  Your
thoughts?

thanks,
-steve

-- 
Steve Vinoski  (508)436-5904   vinoski@apollo.hp.com
Distributed Object Computing Program
Hewlett-Packard, Chelmsford, MA 01824       These are my opinions.
Technical Details
Message-ID: <Bt5ECn.CtF@apollo.hp.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!sdd.hp.com!apollo.hp.com!netnews

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!sdd.hp.com!apollo.hp.com!netnews
From: vinoski@apollo.hp.com (Stephen Vinoski)
Subject: const and template parameters
Originator: vinoski@zep_r.ch.apollo.hp.com
Sender: usenet@apollo.hp.com (Usenet News)
Message-ID: <Bt5ECn.CtF@apollo.hp.com>
Date: Mon, 17 Aug 1992 21:57:58 GMT
Nntp-Posting-Host: zep_e.ch.apollo.hp.com
Organization: Hewlett-Packard Company, Apollo Division - Chelmsford, MA
Lines: 48