Skip to content

Commit 72dabe3

Browse files
Ensure that perfect forwarding is observed in cpp_function lambda wrappers.
1 parent a8dc409 commit 72dabe3

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

include/pybind11/pybind11.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ class cpp_function : public function {
7070
/// Construct a cpp_function from a class method (non-const)
7171
template <typename Return, typename Class, typename... Arg, typename... Extra>
7272
cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
73-
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
73+
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
7474
(Return (*) (Class *, Arg...)) nullptr, extra...);
7575
}
7676

7777
/// Construct a cpp_function from a class method (const)
7878
template <typename Return, typename Class, typename... Arg, typename... Extra>
7979
cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
80-
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
80+
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
8181
(Return (*)(const Class *, Arg ...)) nullptr, extra...);
8282
}
8383

tests/test_methods_and_attributes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55

66
def test_methods_and_attributes():
7-
instance1 = m.ExampleMandA()
8-
instance2 = m.ExampleMandA(32)
7+
instance1 = m.ExampleMandA() # 1 ctor
8+
instance2 = m.ExampleMandA(32) # 1 ctor
99

10-
instance1.add1(instance2)
10+
instance1.add1(instance2) # 1 copy ctor + 1 move
1111
instance1.add2(instance2)
1212
instance1.add3(instance2)
1313
instance1.add4(instance2)
@@ -20,7 +20,7 @@ def test_methods_and_attributes():
2020

2121
assert str(instance1) == "ExampleMandA[value=320]"
2222
assert str(instance2) == "ExampleMandA[value=32]"
23-
assert str(instance1.self1()) == "ExampleMandA[value=320]"
23+
assert str(instance1.self1()) == "ExampleMandA[value=320]" # 1 copy ctor + 1 move
2424
assert str(instance1.self2()) == "ExampleMandA[value=320]"
2525
assert str(instance1.self3()) == "ExampleMandA[value=320]"
2626
assert str(instance1.self4()) == "ExampleMandA[value=320]"
@@ -58,8 +58,8 @@ def test_methods_and_attributes():
5858
assert cstats.alive() == 0
5959
assert cstats.values() == ["32"]
6060
assert cstats.default_constructions == 1
61-
assert cstats.copy_constructions == 3
62-
assert cstats.move_constructions >= 1
61+
assert cstats.copy_constructions == 2
62+
assert cstats.move_constructions == 2
6363
assert cstats.copy_assignments == 0
6464
assert cstats.move_assignments == 0
6565

0 commit comments

Comments
 (0)