Article View: comp.std.c++
Article #104const
From: "Don McCrady"
Date: Tue, 14 Apr 1992 19:06
Date: Tue, 14 Apr 1992 19:06
41 lines
1820 bytes
1820 bytes
> 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.
Technical Details
Message-ID:
<9204141917.AA29803@ucbvax.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!ucbvax!TOROLAB6.VNET.IBM.COM!MCCRADYShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!ucbvax!TOROLAB6.VNET.IBM.COM!MCCRADY
From: MCCRADY@TOROLAB6.VNET.IBM.COM ("Don McCrady")
Newsgroups: comp.std.c++
Subject: const
Message-ID: <9204141917.AA29803@ucbvax.Berkeley.EDU>
Date: 14 Apr 92 19:06:01 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Lines: 42