Re: struct_pod - what struct should have been
"James Dennett" <jdennett@acm.org> wrote in message
news:BhUpg.845$lv.10@fed1read12...
The thing is that it's *not* POD -- POD has a definition,
Sorry, I've been assuming it's obvious: The definition of POD would be
amended to include 'struct_pod'.
Some languages would prefer to do that with a pragma, and
that might also be a reasonable way to do it for a C++
compiler.
Well, no. This is a significant change.
struct_pod MyPod
{ int n;
MyPod& operator=(int n) { bar(); } // Valid in POD today
MyPod& operator=(const MyPod& m) { foo(); } // Would become valid
};
union U
{ int x;
MyPod m;
};
U a,b;
a.x=2;
b.x=5;
b.m=6; // bar() is called
a.m=b.m; // foo() is called
This could not be done without struct_pod.
I don't think there's any chance of something
like this being standardized until/unless it's implemented
in some compilers, so you might be better to work on getting
it used in the real world before trying to device the perfect
syntax for standardization.
It is implemented in compilers. They currently have tests to disable the
implementation that would otherwise occur, e.g. 'If class has assign-copy it
is non-POD - disallow in situations where only POD may occur'.
Few use classes in POD situations precisely because being able to override
the assign-copy operator is pretty essential. NB That doesn't mean that the
class cannot be designed to survive memcpy.
Alternatively, one could use explicit:
struct MyPod
{ int n;
explicit MyPod& operator=(const MyPod& m) { foo(); }
};
Because the assign-copy is marked 'explicit', it does not break the podness
of MyPod.
explicit would also be applicable in the same manner to default
constructors, and destructors.
---
[ 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 ]