Re: Anonymous union rules against constructors & assignment
<johnchx2@yahoo.com> wrote in message
news:1149633364.561734.56320@i39g2000cwa.googlegroups.com...
memcpy doesn't work on non-PODs.
This is because they aren't guaranteed to be laid out anything like you
think they will be.
'memcpy doesn't work on non-PODs' !!!
Wow! That's bad news.
So, let's say I had this:
class A {... }* a;
foo()
{void* p=malloc(sizeof(A));
new(p) A();
a=(A*)malloc(sizeof(A));
memcpy(a,p,sizeof(A)); // Doesn't work if A is non-POD?
free(p);
a->method();
delete a;
}
If memcpy doesn't work on non-POD objects then something truly fundamental
has changed, e.g. C++ has become C#.
I don't care how non-POD objects are laid out, because I don't intend to
exploit their layout. I only require that I have the option that I may
overlay their storage with other data, AND that I be given credit for
knowing that the non-POD objects may require construction with placement
new, may require custom non-default assignment, and may require destruction.
All that's required is changing a compiler error to a warning:
"Error: Union member object must have default assign-copy operator"
to
"Warning: Union member object does not have default assign-copy operator"
That's all. It's such a weeny little thing.
---
[ 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.comeaucomputing.com/csc/faq.html ]