🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Qualification conversions & const safety

#2497
From: Alex Martelli
Date: Fri, 10 Nov 1995 19:25
55 lines
2475 bytes
Greg McGary <gkm@magilla.cichlid.com> writes:

>According to sec. 4.4: Qualification conversions, paragraph 3, the
>following is NOT a const-safe conversion:

>	char * *  --> char const * *

>whereas this is safe:

>	char * *  --> char const *const *

>Why?  I don't see how *adding* `const' an any level of a multi-level
>pointer could lessen safety.

It's very un-intuitive (keeps coming up in various guises in c.l.c++,
too -- and comp.sather, comp.object, comp.<whatever>...:-), but
nevertheless true.  Consider this example:

	const char array[] = "Hello";
	void naive(const char **p) {
	    *p = &array[0];
	}
	void tricky(char **p) {
	    naive(p);
	}
	//...
	char *q;
	tricky(&q);
	*q='P';

What's going on here?  Const safety is clearly violated, since
the last statement tries to change the value of a character
that has been defined as constant; there were no casts anywhere;
_where_ was our safety compromised?

Answer: in tricky()'s call on naive(), where a char** was
transformed into a const char**.  That transformatoin is not safe.

Why?  Because transforming X*->Y* is ok iff Y can be everywhere
substituted for X (Liskov principle); and a "char const *"
cannot be _everywhere_ substituted for a "char *" -- in
particular, it cannot be when it's dereferenced and the
result _changed_.


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <martelli.816016577@cadlab>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <47rvj7$d2i@magilla.magilla.cichlid.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 10 Nov 95 19:25:10 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
From: martelli@cadlab.it (Alex Martelli)
Subject: Re: Qualification conversions & const safety
Message-ID: <martelli.816016577@cadlab>
X-Newsreader: NN version 6.5.0 #11
Organization: Cad.Lab Spa, Bologna, Italia
References: <47rvj7$d2i@magilla.magilla.cichlid.com>
X-Original-Date: Fri, 10 Nov 1995 15:16:17 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMKOnI+EDnX0m9pzZAQHlAgF/T6GBJXsnSfyxDxezj7B5aH8fQIs5mt7a
 68uG4rSYMrTMjRzvp/cz9+yB5Le7vUUM
 =chcm