Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp::Reference ambiguous cast #359

Closed
ibilon opened this issue Jan 11, 2016 · 1 comment
Closed

cpp::Reference ambiguous cast #359

ibilon opened this issue Jan 11, 2016 · 1 comment

Comments

@ibilon
Copy link
Member

ibilon commented Jan 11, 2016

There's a problem when using unsafe cast on cpp::Reference:

extern class A { }
@:native("cpp::Reference<A>") extern clas ARef extends A { }

extern class B exends A { }
@:native("cpp::Reference<B>") extern clas BRef extends B {
@:native("new B") static public function create () : BRef;
}

If we get a BRef we can't directly assign it a variable of type ARef,
but unsafe casting doesn't work either:

var bref : BRef = Bref.create();
var aref : ARef = cast bref;

gives the following error with g++ both with the latest haxelib and github version:

error: call of overloaded ‘Reference(cpp::Reference<B>&)’ is ambiguous
this->aref = ((cpp::Reference<A>)(bref));

and four candidate constructors.

Changing the toolchain to clang gives the same error

error: ambiguous conversion for C-style cast from 'cpp::Reference<B>' to cpp::Reference<A>'
this->aref = ((cpp::Reference<A>)(bref));

This doesn't happen on windows.

A more "real" example can be found in https://github.com/ianharrigan/hxWidgets/blob/master/src/hx/widgets/Frame.hx#L15 (_ref is defined in parent's parent class https://github.com/ianharrigan/hxWidgets/blob/master/src/hx/widgets/EvtHandler.hx#L12)

@hughsando
Copy link
Member

Here is an example that compiles:

extern class A { var a:Int; }
@:native("cpp::Reference<A>") extern class ARef extends A { }
extern class B extends ARef { var b:Int; }

@:native("cpp::Reference<B>") extern class BRef extends B
{
   @:native("new B") static public function create () : BRef;
}

@:cppFileCode("
  struct A { A() { a=1;} int a; };
  struct B : public A { B() { b=2;} int b; };
")
class Test
{
   public static function main()
   {
      var bref : BRef = BRef.create();
      trace(bref.b);
      var aref : ARef = bref;
      trace(aref.a);
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants