www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14298] New: std.typecons.Proxy incorrectly defines opCast


          Issue ID: 14298
           Summary: std.typecons.Proxy incorrectly defines opCast
                    operator, that breaks casting to supertype.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: luk.wrzosek gmail.com

//test case
import std.typecons;
class IA {}
class IB {}
class C : IB {
  IA a;
  mixin Proxy!a;

  public this() {
    a = new IA;

void main() {
  C c = new C;
  assert(c !is null);

  IA a = cast(IA)c;
  assert(a !is null);

  IB b_ok = c;
  assert(b_ok !is null); //implicit cast works

  IB b_not_ok = cast(IB)c;
  assert(b_not_ok !is null); //assert fails on explicit cast.

//Proxy template always casts to Proxy'ied type:
 auto ref opCast(T, this X)() { return cast(T)a; }

Mar 16 2015