cppyy icon indicating copy to clipboard operation
cppyy copied to clipboard

Friend operators are not callable

Open torokati44 opened this issue 3 years ago • 5 comments

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

torokati44 avatar Sep 16 '22 16:09 torokati44

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

wlav avatar Sep 29 '22 01:09 wlav

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

torokati44 avatar Sep 29 '22 17:09 torokati44

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

wlav avatar Sep 30 '22 21:09 wlav

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

saraedum avatar Oct 10 '22 07:10 saraedum

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

torokati44 avatar Oct 10 '22 08:10 torokati44