wlav / cppyy

Other
402 stars 41 forks source link

Friend operators are not callable #95

Open torokati44 opened 2 years ago

torokati44 commented 2 years ago

See this example code:

import cppyy

cppyy.cppdef("""
#include <iostream>

class Foo {
public:
    Foo &operator+=(const Foo &o) { std::cout << "operator +=" << std::endl; return *this; }
    friend Foo operator+(const Foo &a, const Foo &b) { std::cout << "operator +" << std::endl; return a; }
};

void test() {
    Foo f1, f2, f3;
    f1 += f2;
    f3 = f1 + f2;
}

""")

cppyy.gbl.test()
print("----")

f1, f2, f3 = cppyy.gbl.Foo(), cppyy.gbl.Foo(), cppyy.gbl.Foo()
f1 += f2
f3 = f1 + f2

I would expect to see this:

operator +=
operator +
----
operator +=
operator +

Instead, I'm getting this:

operator +=
operator +
----
operator +=
Traceback (most recent call last):
  File "...", line 24, in <module>
    f3 = f1 + f2
NotImplementedError
wlav commented 1 year ago

Yes, this is known: Cling does not currently expose friend declarations, so these are not available for lookup.

torokati44 commented 1 year ago

Hmm, I see... :/ Are you aware of any plans or effort adding this to Cling?

wlav commented 1 year ago

No, I'm not aware of that. Probably have to do that myself at some point ...

saraedum commented 1 year ago

@torokati44 https://github.com/flatsurf/cppyythonizations/blob/master/src/cppyythonizations/operators/arithmetic.py might work as a workaround in your example.

torokati44 commented 1 year ago

Thank you! This is similar to what I had in mind, so it's good to see it actually done somewhere.