🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: char * to const char*&

#1214
From: Andrew Koenig
Date: Thu, 10 Dec 1992 13:26
42 lines
1934 bytes
In article <JPOLITO.92Dec9155725@sysgem1.encore.com> jpolito@sysgem1.encore.com (Jonathan Polito) writes:

> I am curious to what the current wisdom is on accepting char* as a
> valid argument to a function expecting const char*&

It's invalid, though the reason may not be obvious at first glance.

The main thing to realize is that const char*& means "reference to
pointer to const char" and not "reference to constant pointer to
char" as one might think at first.

That means that in order to accept a char* as an argument, it would
be necessary to convert the char* to a temporary of type const char*
and then bind a reference to that temporary.  However, the reference
does not refer to a const object (because a pointer to const is not
itself a const), so it must be bound to a variable of the same type and
not a temporary.

The thing that makes this example tricky is that if we define

	typedef char* T;

then const T and const char* represent different types! -- const T is
"constant pointer to char" and const char* is "pointer to constant char".

The way to write "constant pointer to char" without a typedef it
char *const&, so the following should be OK:

	extern void f(char *const&);

	main()
	{
		char* p = /* some value */;
		f(p);
		// ...
	}

Here, p is of type "pointer to char" and there is no problem binding to
p a "reference to constant pointer to char".
-- 
				--Andrew Koenig
				  ark@europa.att.com
Technical Details
Message-ID: <24377@alice.att.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!newsserver.pixel.kodak.com!laidbak!tellab5!mcdchg!att!att!allegra!alice!ark
References: <JPOLITO.92Dec9155725@sysgem1.encore.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!newsserver.pixel.kodak.com!laidbak!tellab5!mcdchg!att!att!allegra!alice!ark
From: ark@alice.att.com (Andrew Koenig)
Newsgroups: comp.std.c++
Subject: Re: char * to const char*&
Message-ID: <24377@alice.att.com>
Date: 10 Dec 92 13:26:41 GMT
Article-I.D.: alice.24377
References: <JPOLITO.92Dec9155725@sysgem1.encore.com>
Reply-To: ark@alice.UUCP ()
Distribution: comp
Organization: AT&T Bell Laboratories, Murray Hill NJ
Lines: 42