Re: Anonymous union rules against constructors & assignment
johnchx2@yahoo.com wrote:
Greg Herlihy wrote:
C++ already supports zero-sized data members - they are called member
functions.
[snip]
And it is not too difficult to see how the C++'s type system could
implement a union-like replacement. Simply chnage the d0 and d1
instance variables in the example program into class methods; aside
from some minor adjustment to syntax, the client code remains
unchanged.
I mostly agree with this, except...there's an argument to be made that
there's a big difference between changing client code (however small
the change) and not changing client code. A small change that has to
be made in a couple of hundred different places could be a big burden.
(Especially if the change isn't the sort of thing that can be safely
done with text-based search-and-replace.)
Given that the class is apparently (very much) a work-in-progress, it
remains unclear how much existing client code there is - or whether
that code would have to be changed anyway to bring it over to C++.
Moreover, this particular change is not prone to error. Because any
code not updated will fail to compile. So while the change itself would
no doubt be tedious to apply to a large amount of code, the risk of
error is reasonably low.
I do agree that the parenthesized syntax is not ideal. It is more
common in fact for an interface to separate the read and write property
methods into separate functions - instead of overloading them.
And other solutions are no doubt possible. For example, implementing
other types of member functions may also be worth considering.
Overloading the [] operator for an enumerated value could fit well with
certain data models:
Twin u;
u[d1] = 5;
Personally, I think that being able to assign a (non-function) name to
an arbitrary sub-object of a class opens up some interesting design
possibilities. I'm not sure it buys you anything *fundamentally*
different from what you could get with a member function, but I think
that at least some people would find the non-function syntax easier to
work with.
Well, it also possible to "invert" the function call syntax by using
conversion operators:
Twin u;
int i = d1(u);
Here d1 is now "outside" instead of "within" the value object, u. I am
not sure that this approach has much to recommend it though.
But however the solution is implemented, I am sure that we can both
agree that the approach should be one that works within C++'s type
system and not one that tries to work around it.
Greg
---
[ 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 ]