cannot cast
Jonathan M Davis
jmdavisProg at gmx.com
Thu May 3 00:45:23 PDT 2012
On Thursday, May 03, 2012 09:33:01 Namespace wrote:
> On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote:
> > Other, shorter example:
> >
> > [code]
> > import std.stdio, std.traits;
> >
> > class A {
> >
> > int val;
> >
> > alias val this;
> >
> > T opCast(T : Object)() {
> >
> > writeln("FOO");
> >
> > return to!(T)(this);
> >
> > }
> >
> > }
> >
> > class B : A {
> >
> > }
> >
> > T to(T : Object, U : Object)(const U obj) {
> >
> > return *(cast(T*) &obj);
> >
> > }
> >
> > T const_cast(T)(const T obj) {
> >
> > return cast(T) obj;
> >
> > }
> >
> > void main () {
> >
> > A a = new B();
> > a.val = 42;
> >
> > writefln("a.val: %d", a.val);
> >
> > B* b = cast(B*) &a;
> > writefln("*b.val: %d", b.val);
> >
> > B b1 = to!(B)(a);
> > writefln("b1.val: %d", b1.val);
> >
> > B b2 = cast(B) a;
> > writefln("b2.val: %d", b2.val);
> >
> > const B b3 = cast(B) a;
> >
> > B b4 = const_cast(b3);
> >
> > }
> > [/code]
> >
> > print:
> >
> > alias_this_impl.d(24): Error: function
> > alias_this_impl.A.opCast!(B).opCast () is
> >
> > not callable using argument types ()
> >
> > alias_this_impl.d(44): Error: template instance
> > alias_this_impl.const_cast!(B) e
> > rror instantiating
> >
> > I'm not very skillful in such "template" stories. Maybe someone
> > can help me?
>
> Solved with
>
> T const_cast(T)(const T obj) {
> return to!(T)(obj);
> }
>
> But i think that there must exist a more nicer way to cast away
> const, isn't there?
>
> To cast away "const" with a simple cast to "T" fails (see my post
> above), because i have no idea, how i can restrict my opCast. So
> i have to convert it again with "to". Do some of you have any
> ideas how i can restrict my opCast, so my const_cast doesn't
> match it, e.g. with some template magic?
If you want to restrict opCast, then use a template constraint, constraining
it to what you want to work with it. Also, casting away const is generally a
bad idea in D. Casting away const and mutating a variable is an _extremely_
bad idea. You _really_ shouldn't be doing it. So, the fact that you _have_ a
function which is specifically trying to cast away const is almost certainly
_not_ a good idea.
http://stackoverflow.com/questions/4219600/logical-const-in-d
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list