[Issue 1778] Can not create pointer to class reference

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 18 00:50:26 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1778





------- Comment #7 from aarti at interia.pl  2008-01-18 02:50 -------
> 1) Why is it wrong for real live cases?

Because probably in most cases, when you will need something like pointer to
class you will need also to return it from function. 
And then you have a problem with escaping reference to local variable.

> 3) The return "worked" because the value still existed, undamaged, on the
> stack. The stack cleanup just adjusts the ESP, it doesn't actually stomp on the
> values

Well it confirms that it was just plain luck, that program was working...

4) (answer to your next comment)
class C { }
void test()
{
 C c = new C;
 C* pc = (new C[1]).ptr;
 *pc = c;
}

It seems to be similar workaround like number 1 from my workaround list? And as
I assume it should work when returning from function?

-------------

It's nice that there are workarounds which are actually working right now. But
IMHO they are still only workarounds (quick hacks). And according
to principle "No issue left behind", it should be rather solved than hacked.

Maybe you would consider my proposition:
*** For A being a class, types A and A* should be implicitly castable between
each other. ***

Known consequences (probably just iceberg top, but let me start):
1. In index of associative array: if type of index is A* array should be
indexed by pointer, if type of index is
A, array should be indexed by opHash
2. Proper reductions needs to be applied for types like A**, A*** etc. (see
below)
...

Advantages:
It will reduce number of necessary workarounds in code:

1. In constructor-like functions:
# T* create(T)() {
#   return new T;
# }

instead of currently necessary:
T create(T)() {
    static if (is(T == class))
        return new T;
    else
        return *(new T);
}

2. When storing pointers to class in associative arrays.
Currently I have to cast A to (void*) to store into associative array. With new
solution I would just declare array index as A* and put class there.

3. It solves cleanly problem of creation pointer to class

A** p = new A*;

which, after reduction is same as:
(A*)* p = new (A*); and
A* p = new A;


4. This change is not sophisticated workaround to hide real state of matter,
but rather exposes real state of matter which is that new A; creates pointer
to class A.

Anyway I would say that this bug should stay open, till sane solution will be
found.


-- 



More information about the Digitalmars-d-bugs mailing list