Topic: why is this init disallowed? (const C *&)
Author: juliano@cs.unc.edu (Jeffrey Juliano)
Date: 1999/09/23 Raw View
On 20 Sep 1999 02:32:37 GMT, Siemel B. Naran <sbnaran@uiuc.edu> wrote:
>
>The FAQ does discuss this issue, but you'll have to look in the section
>for why the implicit cast from "T * *" to "T const * *" is illegal.
>Then recall that a reference is basically a pointer, and you'll see
>why [1] is illegal.
thanks for the answer.
perhaps words can be added to the FAQ pointing out that my case is the
same as the one covered by this FAQ question?
I did a search through the FAQ for everything I could think of, but
must have passed over this one and not realized it's the same thing.
And I read the "T * *" to "T const * *" section a couple months ago.
Now I feel stupid. But I bet others will miss this too.
thanks again,
-jeff
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: juliano@cs.unc.edu (Jeffrey Juliano)
Date: 1999/09/19 Raw View
I've run into library code that won't compile with some newer
compilers. Inferring from Stroustrup pp 98, I think the newer
compilers are in conformance with the standard on this issue.
However, I don't understand why this should be disallowed. I'd
appreciate if someone can explain for me.
The code looks something like this:
void foo( const char * & );
void bar( char * p )
{
foo( p ); // [1] doesn't compile
const char * q = p;
foo( q ); // [2] compiles
}
Assuming [1] really is illegal, I have two guesses why
* "char*" and "const char*" are not required to have identical
storage
* it's an artifact of the way the pertinent sections are written
(figured it's about time I buy myself a copy and look it up, but the
ansi online store is currently down due the hurricane floyd. Shatters
my mental model of online services as mysterious a-geographical
entities.)
thanks in advance,
-jeff
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/20 Raw View
On 19 Sep 1999 15:33:41 GMT, Jeffrey Juliano <juliano@cs.unc.edu> wrote:
> void foo( const char * & );
>
> void bar( char * p )
> {
> foo( p ); // [1] doesn't compile
>
> const char * q = p;
> foo( q ); // [2] compiles
> }
The FAQ does discuss this issue, but you'll have to look in the section
for why the implicit cast from "T * *" to "T const * *" is illegal.
Then recall that a reference is basically a pointer, and you'll see
why [1] is illegal.
Here is the FAQ again, but this example is better because it uses long
variable names.
const int const_value=3;
int * pointer=0;
int const * * pointer__pointer=&pointer; // illegal, but suppose ok
*pointer__pointer=&const_value; // alert: same as "pointer=&const_value"
*pointer=5; // oops: modify const_value!
The rule that "T * *" to "T const * *" is illegal keeps us from making
the mistake of modifying a variable that was originally declared const.
If you really know what you are doing, then use const_cast:
void foo( const char * & );
void bar( char * p )
{
foo( const_cast<char const * &> (p) ); // [1] ok
}
>Assuming [1] really is illegal, I have two guesses why
>
> * "char*" and "const char*" are not required to have identical
> storage
On the contrary, "char*" and "const char*" must have identical storage.
--
--------------
siemel b naran
--------------
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/09/20 Raw View
Jeffrey Juliano <juliano@cs.unc.edu> wrote:
> However, I don't understand why this should be disallowed. I'd
> appreciate if someone can explain for me.
>
> The code looks something like this:
>
> void foo( const char * & );
>
> void bar( char * p )
> {
> foo( p ); // [1] doesn't compile
>
> const char * q = p;
> foo( q ); // [2] compiles
> }
Here's why the language doesn't allow it:
const char message[] = "A is the first letter of the alphabet";
void foo(const char*& r)
{
r = message;
}
void bar()
{
char* p = 0;
foo(p);
p[0] = 'Z';
}
The code above would result in an attempt to modify the message, which
results in undefined behavior.
-- Darin
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]