Saturday, April 25, 2015

Digest for comp.lang.c++@googlegroups.com - 7 updates in 2 topics

Daniel Davidson <phyton.dbd@gmail.com>: Apr 25 08:22AM -0700

On Thursday, April 23, 2015 at 4:58:44 PM UTC-5, Alain Ketterlin wrote:
curs in the one case and not in the other?
> view on the question). But this does not mean that it is easy to find
> something valuable to add...
 
> -- Alain.
 
This is great! Thank you Alain
Jorgen Grahn <grahn+nntp@snipabacken.se>: Apr 25 07:06PM

On Thu, 2015-04-23, Daniel Davidson wrote:
 
> I should not have said hand-written. This question theoretical - but
> the setting is actually code generation versus templates. Generated
> code is easier to understand/debug, etc.
 
Isn't one of the main problems with generated code that it's /hard/ to
understand and debug? If you have a buggy code generator (compiler
which generates C++, really) you're going to have major problems.
 
> But I want to find if and
> when templates can outperform.
 
It's an interesting question, but I don't have any answers.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Daniel Davidson <phyton.dbd@gmail.com>: Apr 25 01:47PM -0700

On Saturday, April 25, 2015 at 2:06:51 PM UTC-5, Jorgen Grahn wrote:
 
> Isn't one of the main problems with generated code that it's /hard/ to
> understand and debug? If you have a buggy code generator (compiler
> which generates C++, really) you're going to have major problems.
 
Well, you are asking for an opinion. I'm using tools that make the code fairly straightforward -
https://pub.dartlang.org/packages/ebisu_cpp
 
I just want to know case where I may be giving something up.
 
 
 
Paul <pepstein5@gmail.com>: Apr 25 11:28AM -0700

Code from C++ Primer is:
 
const char* pc;
char * p = const_cast<char*>(pc); // Ok but writing through p is undefined.
 
Why is writing through p undefined? I thought this was an archetypal use of const_cast -- create a non-const object from a low-level const object.
 
Why is this use of const_cast wrong?
 
Paul
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 25 02:44PM -0400

On 4/25/2015 2:28 PM, Paul wrote:
 
> const char* pc;
> char * p = const_cast<char*>(pc); // Ok but writing through p is undefined.
 
> Why is writing through p undefined? I thought this was an archetypal
use of const_cast -- create a non-const object from a low-level const
object.
 
> Why is this use of const_cast wrong?
 
The "Code from C++ Primer" is incomplete to be right or wrong. The
pointer named 'pc' is uninitialized. Writing through 'p' or even
reading through 'pc' yields undefined behavior.
 
V
--
I do not respond to top-posted replies, please don't ask
Jorgen Grahn <grahn+nntp@snipabacken.se>: Apr 25 06:56PM

On Sat, 2015-04-25, Victor Bazarov wrote:
 
> The "Code from C++ Primer" is incomplete to be right or wrong. The
> pointer named 'pc' is uninitialized. Writing through 'p' or even
> reading through 'pc' yields undefined behavior.
 
Yes. Such an example needs to have an actual object to be useful:
 
char foo = 'a';
const char* pc = &foo;
char* p = const_cast<char*>(pc); // Ok
 
const char foo = 'a';
const char* pc = &foo;
char* p = const_cast<char*>(pc); // "Ok but" according to the book
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Dombo <dombo@disposable.invalid>: Apr 25 10:05PM +0200

Op 25-Apr-15 20:28, Paul schreef:
 
> Why is writing through p undefined? I thought this was an archetypal
> use of const_cast -- create a non-const object from a low-level const
> object.
 
Nope, const_cast does not create a new non-const object, it only tells
the compiler: "you may think this is a pointer to a const object, but
take my word for it: it really points to a non-const object". You'd
better be right, or the compiler will have its revenge.
 
> Why is this use of const_cast wrong?
 
pc is not initialized in the example; the effects of reading or writing
through an uninitialized pointer is undefined.
 
But even if it were initialized writing to it is still undefined if pc
is pointing to a const object (this is probably the point C++ Primer is
trying to make). const objects may have been been placed in read-only
memory, making it impossible to modify them.
 
const_cast<> should only be used to cast away constness if you are
absolutely sure that the object being referred to really is non-const.
For example:
 
int main()
{
char[1] data; // non-const object
foo(data);
return 0;
}
 
void foo(const char* pc)
{
bar(pc);
}
 
void bar(const char* pc)
{
char* p= const_cast<char*>(pc);
 
p[0] = '!'; // Ok, because pc is really pointing to a non-const object
}
 
Normally you should never have to use const_cast unless there is some
code you cannot change that passes pointers it got from your code back
to your code. If you need to cast it often indicates there a design issue.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: