Hiding class pointers -- was it a good idea?
Matthias Walter
walter at mail.math.uni-magdeburg.de
Thu Aug 16 02:15:43 PDT 2007
Bill Baxter Wrote:
> eao197 wrote:
> > On Thu, 16 Aug 2007 01:35:17 +0400, Walter Bright
> > <newshound1 at digitalmars.com> wrote:
> >
>
> >> 3) Value types just don't work for polymorphic behavior. They must be
> >> by reference. There's no way in C++ to ensure that your class
> >> instances are used properly by reference only (hence (2)). In fact, in
> >> C++, it's *extra work* to use them properly.
> >
> > But from another side all types in C++ (internal or user defined) is a
> > first class citizens. It is especially important in generic programming,
> > where I can write:
> >
> > template< class T >
> > class ValueHolder {
> > T * m_value
> > public :
> > ValueHolder() : m_value( new T() ) {}
> > ...
> > };
> >
> > and this code will work as with int, as with std::string, as with
> > ValueHolder<SomeAnotherType>.
>
> I hear you. Fortunately it's pretty trivial to throw some
> static-if-is-zzle at the problem in D. May not be so pretty, but it's
> straightforward at least.
>
> --bb
There's an example going to the reverse direction:
I recently worked on a GMP port in D. For this case, one _must_ avoid typing * to ensure that x = b + c; works. (Nobody wants to type *x = *b + *c, I guess)
With C++, there would be a temporary for (b+c) which is assigned to x later - to avoid this copying, the GMP library authors used heavy template metaprogramming (which is ugly to read, although straightforward).
In D I'm happy that I just can write a GMP class, overload the operators and use it as x = b + c; - There is temporary construction, but the assignment of (b+c) to a is just copying the pointer and thus no performance penalty.
What about letting "new int" do nothing and new int (2) be a simple "2"?
Then, T foo = new T(); and T foo = new T(2); would work, too.
best regards
Matthias
More information about the Digitalmars-d
mailing list