[comp.std.c] Rationale for allowing const T* = T* wanted.

rfg@lupine.ncd.com (Ron Guilmette) (02/16/91)

I have been stirring up trouble in x3j16 (the C++ standardization committee)
with a recent suggestion that C++ ought to allow:

		T** p1;
		const T** p2;

			... p2 = p1;

ANSI C type compatability rules do not allow such assignments.  I would
like to see C++ allow them.

In researching my arguments in favor of this proposal, I turned to the
ANSI C standard and to its rationale document to see if x3j11 had considered
this issue already, and (if so) what their reasons were for rejecting it.

The second paragraph under 3.2.2.3 in the ANSI C standard discusses allowable
conversions of pointer type values where the pointed-at types are qualified by
type qualifiers (i.e. `const' or `volatile' or both).

In the copy of the x3j11 rationale I have however, I see no mention at all
of anything relating to this whole topic.

Could somone who was on x3j11 please describe for me why that committee
decided to allow a `T*' to be assigned to a `const T*' while at the same
time making it illegal to assign a `T**' to a `const T**'?

Thanks in advance.

torek@elf.ee.lbl.gov (Chris Torek) (02/19/91)

In article <3913@lupine.NCD.COM> rfg@lupine.ncd.com (Ron Guilmette) writes:
>I have been stirring up trouble in x3j16 [regarding const]

Good :-)

In my opinion, the entire const/non-const setup in ANSI C is wrong.
(I am not entirely happy with it in C++ either, but note that the notion
of `const' is fairly different in the two languages.)

There is something fundamentally `bad' about the fact that, for instance,
a strchr() function must cast a const-pointer to a non-const pointer:

	char *strchr(const char *str, int c0) {
		char c = c0, sc;
		for (; *str != c; str++)
			if (*str == '\0')
				return (NULL);
		return ((char *)str);
	}

In effect, strchr() can be used to `dequalify' a pointer:

	const char *ccp;
	char *cp;

	cp = strchr(ccp, *ccp);	/* dequalify */

If the language is going to provide type checking, strchr() should be
a function of `optionally-const char *' returning `typeof arg1'.
Since it is not, and since example like strchr() are rather more
frequent than I originally expected, I would rather it did not even
pretend to provide such checking.  I believe things would be `better'
if the language freely allowed conversion between `char **' and
`const char **' and `char * const *' and `const char * const *', for
instance.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

gwyn@smoke.brl.mil (Doug Gwyn) (02/20/91)

In article <10034@dog.ee.lbl.gov> torek@elf.ee.lbl.gov (Chris Torek) writes:
>I believe things would be `better' if the language freely allowed
>conversion between `char **' and `const char **' and `char * const *'
>and `const char * const *', for instance.

I think it is recognized that the special meaning given to "pointer
to const" as a function parameter is less than a complete
realization of the basic idea.  Attempts to extend the semantics
past the topmost level of referencing simply didn't come out right,
so we compromised on getting the intention right for one level and
leaving deeper levels less convenient than one might want.  If anyone
can fix this for some other standard such as C++, more power to them.

rfg@NCD.COM (Ron Guilmette) (03/03/91)

In article <10034@dog.ee.lbl.gov> torek@elf.ee.lbl.gov (Chris Torek) writes:
+In article <3913@lupine.NCD.COM> rfg@lupine.ncd.com (Ron Guilmette) writes:
+>I have been stirring up trouble in x3j16 [regarding const]
+
+Good :-)
+
+In my opinion, the entire const/non-const setup in ANSI C is wrong.
+(I am not entirely happy with it in C++ either, but note that the notion
+of `const' is fairly different in the two languages.)
+
+There is something fundamentally `bad' about the fact that, for instance,
+a strchr() function must cast a const-pointer to a non-const pointer:
+
+	char *strchr(const char *str, int c0) {
+		char c = c0, sc;
+		for (; *str != c; str++)
+			if (*str == '\0')
+				return (NULL);
+		return ((char *)str);
+	}
+
+In effect, strchr() can be used to `dequalify' a pointer:

The C++ community already knows all about this leftover problem from C.
Fortunately, this can be (and probably will be) simply solved in the
C++ standard (in the library part that is).

In C++, we don't have to suffer this problem because we can *overload*
and provide two different versions of strchr(), like:

	const char *strchr (const char *, int);
	      char *strchr (      char *, int);

The one which actually gets called will depend upon the types of the
actual parameters in the call.

Happy now?

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// New motto:  If it ain't broke, try using a bigger hammer.