C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: conversion from const char * to const char *&

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Bronek Kozicki
Guest





PostPosted: Thu Apr 01, 2004 8:57 am    Post subject: Re: conversion from const char * to const char *& Reply with quote



On Wed, 31 Mar 2004 19:10:21 +0000 (UTC), "Jonathan Turkanis" wrote:
Quote:
void f(const char*& in_begin, const char* in_end, char*&
out_begin, char* out_end);


Quote:
Now suppose I have a buffer of size n:

char* buf;

It follows from the above that I'm not allowed to invoke f as follows:

f(buf, buf + n, ....)

right? To call f, I have to do

f(const_cast<const char*&>(buf), cbuf + n, ...);

or

const char* cbuf = ptr;
f(cbuf , cbuf + n, ...);

That's a lot of work just to invoke a function with a seemingly
reasonable interface. What's gone wrong here?

Hello Jonathan

const_cast is dangerous. If you see need for it, probably there's
something wrong with code. Consider this:

#include <cstring>
#include <cstdio>

void f(const char *&p)
{
const char *c = "dead";
printf("%st%pn", p, p);
p = c;
}

int main()
{
static const int size = 5;
char *p = new char[size];
strncpy(p, "abcd", size);

// f(p); invalid initialization of non-const reference
f(const_cast<const char*&> (p)); //danger

printf("%st%pn", p, p);
strncpy(p, "beef", size); //boom
printf("%st%pn", p, p);
}

Here const_cast has been used the same way you are going to obey
limitation of non-const reference initialization in your example cited
above. This reference is used later to circumvent constness of another
variable. There are few compilers (eg. all versions of Visual C++, when
started without option /Za) that will accept line I have commented out:
f(p);
due to this compiler err^H non-conformance issue above code will violate
const variable without single const_cast.

I think that you should remove reference from first parameter of your
function:

void f(const char* in_begin, const char* in_end, char*& out_begin /* */)

this will make your function easy and safe to use. Alternatively you may
use const reference:

void f(const char* const& in_begin, const char* in_end, /* ... */)


Regards


B.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bronek Kozicki
Guest





PostPosted: Thu Apr 01, 2004 10:35 am    Post subject: Re: conversion from const char * to const char *& Reply with quote



On Wed, 31 Mar 2004 20:56:13 +0000 (UTC), "Jonathan Turkanis" wrote:

Quote:
char*& source();
[...]
void sink(const char*&);
[...]
sink(source()); // error.

The first bullet of 8.5.3/5 does not apply, because of reference
incompatibility; the second bullet ('--Otherwise') does not apply,
since const char* is non-const.

Right. Actually the only part of C++ standard that applies here is last
sentence of this clause: "otherwise, the program is ill-formed" (just
before final example and note).

Quote:
The compiler diagnostic about binding non-const references to
temporaries comes from attempting to bind the result of converting the
resturn value of source() to const char*.

Result of such conversion cannot be bound, because rvalue cannot be
bound to non-const reference; and result of any standard conversion is
rvalue. If some compiler tries to perform conversion and then finds that
result of such conversion cannot be bound to reference it's OK, but it's
equally valid to reject code due to first bullet of clause 8.5.3/5 of
C++ standard, as there is no other bullet which could allow binding to
non-const reference. It's just quality of implementation issue, but (as
ill-formed code has been rejected by compiler) result is the same, as
mandated by said clause. Actually Comeau compiler issues following error
message:

"T.cpp", line 25: error #434: a reference of type "const char *&" (not
const-qualified) cannot be initialized with a value of type "char *"
sink(source()); // error.


Quote:
However, the compiler could
just as reasonably have complained about violation of the condition in
the first bullet. Right?

You got it right :)

Quote:
I'm still having trouble understanding why the code above is unsafe.

See my other message in this thread. Best regards


B.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.