Skip to content

Commit 782c47a

Browse files
[WIP] New Overload Resolution
1 parent d29e7a9 commit 782c47a

File tree

3 files changed

+40
-75
lines changed

3 files changed

+40
-75
lines changed

test/test_advancedcpp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def test19_comparator(self):
679679
assert a.__eq__(a) == False
680680
assert b.__eq__(b) == False
681681

682-
@mark.xfail(condition=IS_MAC, reason="Fails on OS X")
682+
@mark.xfail(reason="Behaviour dependent on order of overload definition")
683683
def test20_overload_order_with_proper_return(self):
684684
"""Test return type against proper overload w/ const and covariance"""
685685

test/test_overloads.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,42 @@ class E: public D {};
288288

289289
assert ns.myfunc2(ns.E()) == "E"
290290
assert ns.myfunc2(ns.D()) == "D"
291+
292+
def test14_disallow_functor_to_function_pointer(self):
293+
"""Make sure we're no allowing to convert C++ functors to funciton
294+
pointers, extending the C++ language in an unnatural way that can lead
295+
to wrong overload resolutions."""
296+
297+
import cppyy
298+
299+
cppyy.cppdef("""
300+
class Test14Functor {
301+
public:
302+
double operator () (double* args, double*) {
303+
return 4.0 * args[0];
304+
}
305+
};
306+
int test14_foo(double (*fcn)(double*, double*)) {
307+
return 0;
308+
}
309+
template<class T>
310+
int test14_foo(T fcn) {
311+
return 1;
312+
}
313+
int test14_bar(double (*fcn)(double*, double*)) {
314+
return 0;
315+
}
316+
int test14_baz(double (*fcn)(double*, double*)) {
317+
return 0;
318+
}
319+
int test14_baz(std::function<double(double*, double*)> const &fcn) {
320+
return 2;
321+
}
322+
""")
323+
324+
functor = cppyy.gbl.Test14Functor()
325+
assert cppyy.gbl.test14_foo(functor) == 1 # should resolve to foo(T fcn)
326+
# not allowed, because there is only an overload taking a function pointer
327+
raises(TypeError, cppyy.gbl.test14_bar, functor)
328+
# The "baz" function has a std::function overload, which should be selected
329+
assert cppyy.gbl.test14_baz(functor) == 2 # should resolve to baz(std::function)

test/test_templates.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,80 +1288,6 @@ def test37_enum_template_argument_function(self):
12881288
assert gbl.get[gbl.What.NO]() == 0
12891289
assert gbl.get[gbl.What.YES]() == 1
12901290

1291-
def test37_enum_template_argument_function(self):
1292-
import cppyy
1293-
from cppyy import gbl
1294-
1295-
cppyy.cppdef(
1296-
r"""
1297-
struct ERDataFrame {
1298-
size_t rows = 0;
1299-
std::vector<double> cols;
1300-
1301-
private:
1302-
ERDataFrame(size_t n, std::vector<double> c) : rows(n), cols(c) {}
1303-
1304-
public:
1305-
ERDataFrame() {}
1306-
template <typename R, typename... T>
1307-
ERDataFrame Define(std::string name, R (*f)(T...)) {
1308-
auto copy = cols;
1309-
size_t I = rows;
1310-
auto args = std::tuple{(static_cast<T>(cols[--I]))...};
1311-
R res = std::apply(f, args);
1312-
// std::cout << "Adding column " << rows + 1 << " " << name << " value " << res << std::endl;
1313-
copy.push_back(res);
1314-
return ERDataFrame(rows + 1, copy);
1315-
}
1316-
};
1317-
1318-
double get_one() { return 1.0; }
1319-
double plus_one(double x) { return x + 1.0; }
1320-
double add(double x, double y) { return x + y; }
1321-
double add3(double x, double y, double z) { return x + y + z; }
1322-
double throw_error() { throw std::runtime_error("called throw_error"); }
1323-
"""
1324-
)
1325-
1326-
1327-
class CallBackError(Exception):
1328-
pass
1329-
1330-
1331-
def callback(x: int) -> float:
1332-
return x * 2.0
1333-
1334-
1335-
def raise_error() -> float:
1336-
raise CallBackError("called raise_error")
1337-
1338-
1339-
o = gbl.ERDataFrame()
1340-
assert o.rows == 0
1341-
o = o.Define("col1", gbl.get_one)
1342-
assert o.rows == 1
1343-
o = o.Define("col2", gbl.plus_one)
1344-
assert o.rows == 2
1345-
o = o.Define("col3", gbl.add)
1346-
assert o.rows == 3
1347-
o = o.Define("col4", gbl.add3)
1348-
assert o.rows == 4
1349-
o = o.Define("col5", callback)
1350-
assert o.rows == 5
1351-
assert o.cols[0] == 1
1352-
assert o.cols[1] == 2
1353-
assert o.cols[2] == 3
1354-
assert o.cols[3] == 6
1355-
assert o.cols[4] == 12
1356-
1357-
# with raises(CallBackError):
1358-
# o.Define("errA", raise_error) # FIXME: raises TypeError for failure in overload selection
1359-
1360-
# with raises(gbl.std.runtime_error):
1361-
# o.Define("errB", gbl.throw_error) # FIXME: raises TypeError for failure in overload selection
1362-
1363-
assert o.rows == 5
1364-
13651291

13661292
@mark.skipif((IS_MAC and IS_CLING), reason="setup class fails with OS X cling")
13671293
class TestTEMPLATED_TYPEDEFS:

0 commit comments

Comments
 (0)