Topic: Clarification of multi-level const addition
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1996/04/25 Raw View
> Where in C,
>
>int * --> const int * [is legal]
cv1 T* -> cv2 T* iff cv1 <= cv2
>int * * --> const int * * [is illegal]
>int * * --> const int * const * [is illegal]
>
>... a different rule now applies in C++. The wording is some of the
>toughest legalese in the standard, and i'm not sure exactly what it means.
>To quote [4.4]:
[skiped]
>This means that:
>int * --> const int * [is still legal]
Right, legal C and C++
>int * * --> const int * * [is still illegal (?)]
Right, illegal C and C++
>int * * --> const int * const * [is now legal]
Right, illegal C but now legal C++
>int * * --> const int * const * const [is now legal]
The same (changing cv-qualifier is a trivial conversion for a r-value)
>int * * --> int * const * [is legal (?), was illegal (?)]
Wrong, legal C and C++, the very simple rule T* -> const T* apply
The C++ rule can be expressed recursivly (please correct me if I am wrong):
A cv1 * -> B cv2 * (cv-convert to)
iff (1) cv2 >= cv1
(2) A -> B (cv-convert to) and cv2 is const
This rule is not consistent, in my opinion, it should be
(2) A -> B (convert to) and cv2 is const
in order to convert Derived * cv * to Base * const cv * where Base is an the first
Base of Derived or void.
My idea would imply that the following code work:
public Base {};
class Derived : public Base, SecondBase {};
Derived d;
Base *bp = &d;
SecondBase *sbp = &d;
assert ((long)&d == (long)bp);
assert ((long)&d == (long)sbp); // will fail
// given of course that sizeof (long) >= sizeof (void*)
I'm not sure if the standard garanty it.
Note to implementors: compilers often garanty things which are not in the standard;
if they do, they should allow the conresponding implicits conversions if the ANSI
option is OFF.
-----------------------------
My opinions are mines.
Valentin Bonnard
bonnardv@pratique.fr
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bradd W. Szonye" <bradds@ix.netcom.com>
Date: 1996/04/23 Raw View
In reading the DWP, I've noticed that multi-level conversion from
non-const/non-volatile to "more-qualified" types has changed from
ISO/9899-1990. Where in C,
int * --> const int * [is legal]
int * * --> const int * * [is illegal]
int * * --> const int * const * [is illegal]
... a different rule now applies in C++. The wording is some of the
toughest legalese in the standard, and i'm not sure exactly what it means.
To quote [4.4]:
(1) An rvalue of type "pointer to cv1 T" can be converted to an rvalue of
type "pointer to cv2 T" if "cv2 T" is more cv-qualified than "cv1 T."
[This is the C rule.]
(2) An rvalue of type "pointer to member of X of type cv1 T" can be
converted [... A simple variation for pointers to members.]
(3) A conversion can add type qualifiers at levels other than the first in
multi-level pointers, subject to the following rules:
Two pointer types T1 and T2 are similar if there exists a type T and
integer N>0 such that:
T1 is Tcv1,n * ... cv1,1 * cv1,0
and
T2 is Tcv2,n * ... cv2,1 * cv2,0
where each cvi,j is const, volatile, const volatile, or nothing. An
expression of type T1 can be converted to type T2 iff:
-- the pointer types are similar.
-- for every j>0, if const is in cv1,j then const is in cv2,j, and
similarly for volatile.
-- if the cv1,j and cv2,j are different, then const is in every cv2,k for
0<k<j.
This means that:
int * --> const int * [is still legal]
int * * --> const int * * [is still illegal (?)]
int * * --> const int * const * [is now legal]
int * * --> const int * const * const [is now legal]
int * * --> int * const * [is legal (?), was illegal (?)]
I think this is the right interpretation, but I'm not sure. If anyone can
confirm this for certain, I'd like to know. In particular, I'm curious
about the second example, int * * -> const int * *, which seems like a
reasonable thing to do, but appears to be illegal. In contrast, I'm not
sure what the last example is supposed to mean at all, or why it's an
improvement over the C rule.
Have these rules changed in the last year?
--
Bradd W. Szonye (bradds@ix.netcom.com), Doubleplus Corporation
"To chill or to pop a cap in my dome, whoomp, there it is."
-- Hamlet, Prince of Denmark
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]