what operator(s) should I overload to prevent this?
Steven Schveighoffer
schveiguy at yahoo.com
Mon Oct 16 16:48:48 UTC 2017
On 10/16/17 8:00 AM, drug wrote:
> I refactored `MyStructure` added own implementation of malloced array
> based on pureRealloc/pureFree instead of dynamic array I used before and
> now I have error:
> Error: cannot implicitly convert expression get(msg.getData()) of type
> const(MyStructure) to MyStructure.
>
> What operators should I overload to let this conversion? I've tryed
> opCast and it fails in other call sites, also I've tried opAssign - it
> didn't works, what worked is wrapping in ctor - MyStruct(const_var). But
> using ctor is not convenient.
>
The issue is:
int[] -> const(int)[].
This is an implicit cast from a mutable array of mutable data to a
mutable array of const data. There is no way to "hook" this properly.
What you need is:
MyArray!int -> MyArray!(const(int))
But D doesn't do this implicitly. It can't, as you could implement
MyArray!(const(int)) to be completely different from MyArray!int.
If you define opCast(T : MyArray!(const(T))), then it requires an
explicit cast, you need an implicit one. You could use alias this, but I
don't think it's as smooth.
Complicating things further, you have template functions like this:
foo(T)(const(T)[])
Where D is smart enough to pull out the T there. I don't think it works
with MyArray.
-Steve
More information about the Digitalmars-d-learn
mailing list