On 04.12.2010 6:23, Andrei Alexandrescu wrote:
On 12/3/10 7:40 PM, Michel Fortin wrote:
I have an idea that would fix those: make a template struct/class
instance implicitly convertible to another instance of that same
template if all members share the same memory layout and each member is
implicitly convertible to the same member of the other template.

I'm afraid that can't work.

struct A(T) {
    T obj;
    void fun() { obj->method(); }
}


Looks discouraging at first, but perfectly valid given that the example works only when A!Object compiles, i.e. Object have method 'method', and then:

auto a = new A!Widget;
a.obj = new Widget;
A!Object b = *a; // works because Widget converts to Object
// A!Object* b = a; // should not compile, and would be a problem if A is a class
//now we have another struct b with reference to a's widget
b.obj = new Object; //no problem, a stays intact
b.fun(); // since A!Object already compiles, it's perfectly valid

In fact, it looks like Michel's rule is very promising, just replace "struct/class" part with "struct" in definition.

--
Dmitry Olshansky

Reply via email to