On 05/23/2010 05:06 PM, bearophile wrote:
Andrei Alexandrescu:

1. Using a class makes implementing members easier because there's no
need to do work through an additional member. With a struct, you need a
pimpl approach. For example:

struct Array {
      struct Impl {
          ...
      }
      Impl * impl;
      ...
      @property length() const { return impl->length; }
      ...
}

I am far from being an expert as you, and while I have read about the PImpl in 
C++, I don't understand what you are saying here. I am sorry.

If you want to define a class you go like this:

class CBlah
{
    private int payload_;
    @property int payload() { return payload_; }
    @property void payload(int p) { payload_ = p; }
}

Right? The class has by birth reference semantics:

auto obj = new CBlah;
obj.payload = 42;
auto obj1 = obj;
obj1.payload = 43;
assert(obj.payload == 43);

If you want to define a struct with ref semantics you define it like this:

struct SBlah
{
    private struct Impl
    {
        private int payload_;
    }
    private Impl * pImpl;
    @property int payload() { return pImpl->payload_; }
    @property void payload(int p) { pImpl->payload_ = p; }
}

auto obj = SBlah();
obj.pImpl = new SBlah.Impl; // meh, a crappy detail that we need to
                            // discuss later
obj.payload = 42;
auto obj1 = obj;
obj1.payload = 43;
assert(obj.payload == 43);

That's what I said.

3. The creation syntaxes are different.

In C# you use 'new' for both structs allocated on the stack and classes 
allocated on the heap:
http://msdn.microsoft.com/en-us/library/51y09td4%28VS.71%29.aspx

That's pretty retarded (sorry retard). There was opportunity there to get rid of new altogether.

Let me note that I have reached the conclusion that containers should
be at best reference types, with a meta-constructor Value!(C) that takes
a container C and makes it into a value type.

Does Value!() use static introspection and placement new to instantiate the 
given class on the stack? :-)

No. It does copy it automatically. There should be no notable difference between Value!(Array!int) and std::vector<int>.


Andrei

Reply via email to