SomeRanDev / reflaxe.CPP

An alternative C++ target for Haxe that generates dependent-less, GC-less C++17 code.
MIT License
72 stars 5 forks source link

function bind was wrong. #25

Open sonygod opened 1 year ago

sonygod commented 1 year ago
class Test {

    public function new() {

    }

    public function say(){

        trace('hello world');
    }
}
....
var test4=new Test();

var fn=test4.say;

fn();

and get this cpp code

std::shared_ptr<Test> test4 = std::make_shared<Test>();
    std::function<void()> fn = test4->say;//wrong here
    fn();

should be


    std::function<void(void)> fn = std::bind(&Test::say, test4);

     // or  which is the better choose?
        auto fn2 = std::mem_fn(&Test::say);
         fn2(test4.get());
SomeRanDev commented 1 year ago

Oooh, nice catch!! I like your solutions too! Wasn't aware of those functions and will definitely use those!!

I was initially thinking to just generate a lambda. But that'd look worse. XD

auto fn2 = []() { test4->say(); };