Topic: const


Author: MCCRADY@TOROLAB6.VNET.IBM.COM ("Don McCrady")
Date: 14 Apr 92 19:06:01 GMT
Raw View
> From: donne@imec.be (Werner Donne)
>
>    const char **a;
>    char **b;
>
>   a = b;
>
> Why is the assignment illegal?

This has been illegal ever since C, and I assume C++ hasn't relaxed
C's rules.  The relevant rule from the assignment operator, ANSI C
3.3.16.1, is:

    One of the following shall hold:
        ...
        - both operands are pointers to qualified or unqualified
          versions of compatible types... [1]

[1] Omitted is the phrase "and the type pointed to by the left has
all the qualifiers of the type pointed to by the right," which is
irrelevant to this discussion.

The above rule means that we can ignore the qualifiers of the *first*
level of the type, but that all the types after this level must be
strictly compatible.  This means that we have to determine whether
a "pointer to const char" is compatible with a "pointer to char".
ANSI C 3.5.4.1 states "For two pointer types to be compatible, both
shall be identically qualified and both shall be pointers to compatible
types."  Now we must determine if "const char" is compatible "char".
ANSI C 3.5.3 states "For two qualified types to be compatible, both shall
have the identically qualified vesion of a compatible type..."

Note that by similar reasoning, all of the following are illegal:

       b = a;
       a == b;  /* or !=, or <, etc... */
       b == a;  /* as above */

++don;

IBM Canada, Toronto.





Author: darcy@druid.uucp (D'Arcy J.M. Cain)
Date: 15 Apr 92 14:59:31 GMT
Raw View
MCCRADY@TOROLAB6.VNET.IBM.COM ("Don McCrady") writes:
>> From: donne@imec.be (Werner Donne)
>>    const char **a;
>>    char **b;
>>   a = b;
>> Why is the assignment illegal?
>
>This has been illegal ever since C, and I assume C++ hasn't relaxed
>C's rules.  The relevant rule from the assignment operator, ANSI C
>3.3.16.1, is:

Right so far.

>    One of the following shall hold:
>        ...
>        - both operands are pointers to qualified or unqualified
>          versions of compatible types... [1]
>
>[1] Omitted is the phrase "and the type pointed to by the left has
>all the qualifiers of the type pointed to by the right," which is
>irrelevant to this discussion.

Hardly irrelevant.  The additional phrase says that the LHS must have
at least all of the type qualifiers (const and/or volatile - see 3.5.3)
of the RHS but the opposite isn't necessarily true.
[...]
>Note that by similar reasoning, all of the following are illegal:
>       b = a;
No, this is perfectly legal.  That's what the additional phrase means.
The RHS has no type qualifier so the LHS doesn't need one but the existence
of one doesn't violate the rule.

>       a == b;  /* or !=, or <, etc... */
>       b == a;  /* as above */
These are legal but from a different but similar rule in 3.3.9.

I'm not sure why the original question was posted to comp.std.c++ as well
as comp.std.c but I think this is a C standard discussion more than a C++
one.  Followups redirected.  Feel free to change it again if you see any
C++ specific issue.

--
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871          DoD#0082  |