Skip to content

Commit eb2660b

Browse files
committed
More perfect forward
1 parent f9cc695 commit eb2660b

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

include/pybind11/detail/init.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,22 @@ void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
201201
template <typename... Args>
202202
struct constructor {
203203
template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
204-
static void execute(Class &cl, const Extra &...extra) {
204+
static void execute(Class &cl, Extra &&...extra) {
205205
cl.def(
206206
"__init__",
207207
[](value_and_holder &v_h, Args... args) {
208208
v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
209209
},
210210
is_new_style_constructor(),
211-
extra...);
211+
std::forward<Extra>(extra)...);
212212
}
213213

214214
template <
215215
typename Class,
216216
typename... Extra,
217217
enable_if_t<Class::has_alias && std::is_constructible<Cpp<Class>, Args...>::value, int>
218218
= 0>
219-
static void execute(Class &cl, const Extra &...extra) {
219+
static void execute(Class &cl, Extra &&...extra) {
220220
cl.def(
221221
"__init__",
222222
[](value_and_holder &v_h, Args... args) {
@@ -229,23 +229,23 @@ struct constructor {
229229
}
230230
},
231231
is_new_style_constructor(),
232-
extra...);
232+
std::forward<Extra>(extra)...);
233233
}
234234

235235
template <
236236
typename Class,
237237
typename... Extra,
238238
enable_if_t<Class::has_alias && !std::is_constructible<Cpp<Class>, Args...>::value, int>
239239
= 0>
240-
static void execute(Class &cl, const Extra &...extra) {
240+
static void execute(Class &cl, Extra &&...extra) {
241241
cl.def(
242242
"__init__",
243243
[](value_and_holder &v_h, Args... args) {
244244
v_h.value_ptr()
245245
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
246246
},
247247
is_new_style_constructor(),
248-
extra...);
248+
std::forward<Extra>(extra)...);
249249
}
250250
};
251251

@@ -257,15 +257,15 @@ struct alias_constructor {
257257
typename... Extra,
258258
enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int>
259259
= 0>
260-
static void execute(Class &cl, const Extra &...extra) {
260+
static void execute(Class &cl, Extra &&...extra) {
261261
cl.def(
262262
"__init__",
263263
[](value_and_holder &v_h, Args... args) {
264264
v_h.value_ptr()
265265
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
266266
},
267267
is_new_style_constructor(),
268-
extra...);
268+
std::forward<Extra>(extra)...);
269269
}
270270
};
271271

@@ -290,7 +290,7 @@ struct factory<Func, void_type (*)(), Return(Args...)> {
290290
// inheriting from the C++ type) the returned value needs to either already be an alias
291291
// instance, or the alias needs to be constructible from a `Class &&` argument.
292292
template <typename Class, typename... Extra>
293-
void execute(Class &cl, const Extra &...extra) && {
293+
void execute(Class &cl, Extra &&...extra) && {
294294
#if defined(PYBIND11_CPP14)
295295
cl.def(
296296
"__init__",
@@ -306,7 +306,7 @@ struct factory<Func, void_type (*)(), Return(Args...)> {
306306
v_h, func(std::forward<Args>(args)...), Py_TYPE(v_h.inst) != v_h.type->type);
307307
},
308308
is_new_style_constructor(),
309-
extra...);
309+
std::forward<Extra>(extra)...);
310310
}
311311
};
312312

@@ -334,7 +334,7 @@ struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
334334
// The class factory is called when the `self` type passed to `__init__` is the direct
335335
// class (i.e. not inherited), the alias factory when `self` is a Python-side subtype.
336336
template <typename Class, typename... Extra>
337-
void execute(Class &cl, const Extra &...extra) && {
337+
void execute(Class &cl, Extra &&...extra) && {
338338
static_assert(Class::has_alias,
339339
"The two-argument version of `py::init()` can "
340340
"only be used if the class has an alias");
@@ -359,7 +359,7 @@ struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
359359
}
360360
},
361361
is_new_style_constructor(),
362-
extra...);
362+
std::forward<Extra>(extra)...);
363363
}
364364
};
365365

@@ -409,7 +409,7 @@ struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> {
409409
pickle_factory(Get get, Set set) : get(std::forward<Get>(get)), set(std::forward<Set>(set)) {}
410410

411411
template <typename Class, typename... Extra>
412-
void execute(Class &cl, const Extra &...extra) && {
412+
void execute(Class &cl, Extra &&...extra) && {
413413
cl.def("__getstate__", std::move(get));
414414

415415
#if defined(PYBIND11_CPP14)
@@ -427,7 +427,7 @@ struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> {
427427
v_h, func(std::forward<ArgState>(state)), Py_TYPE(v_h.inst) != v_h.type->type);
428428
},
429429
is_new_style_constructor(),
430-
extra...);
430+
std::forward<Extra>(extra)...);
431431
}
432432
};
433433

include/pybind11/operators.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ template <op_id id, op_type ot, typename L, typename R>
8686
struct op_ {
8787
static constexpr bool op_enable_if_hook = true;
8888
template <typename Class, typename... Extra>
89-
void execute(Class &cl, const Extra &...extra) const {
89+
void execute(Class &cl, Extra &&...extra) const {
9090
using Base = typename Class::type;
9191
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
9292
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
9393
using op = op_impl<id, ot, Base, L_type, R_type>;
94-
cl.def(op::name(), &op::execute, is_operator(), extra...);
94+
cl.def(op::name(), &op::execute, is_operator(), std::forward<Extra>(extra)...);
9595
}
9696
template <typename Class, typename... Extra>
97-
void execute_cast(Class &cl, const Extra &...extra) const {
97+
void execute_cast(Class &cl, Extra &&...extra) const {
9898
using Base = typename Class::type;
9999
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
100100
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
101101
using op = op_impl<id, ot, Base, L_type, R_type>;
102-
cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
102+
cl.def(op::name(), &op::execute_cast, is_operator(), std::forward<Extra>(extra)...);
103103
}
104104
};
105105

include/pybind11/pybind11.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class cpp_function : public function {
110110
// NOLINTNEXTLINE(google-explicit-constructor)
111111
cpp_function(std::nullptr_t) {}
112112
cpp_function(std::nullptr_t, const is_setter &) {}
113+
cpp_function(std::nullptr_t, is_setter &&) {}
113114

114115
/// Construct a cpp_function from a vanilla function pointer
115116
template <typename Return, typename... Args, typename... Extra>
@@ -123,9 +124,9 @@ class cpp_function : public function {
123124
typename... Extra,
124125
typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
125126
// NOLINTNEXTLINE(google-explicit-constructor)
126-
cpp_function(Func &&f, const Extra &...extra) {
127+
cpp_function(Func &&f, Extra &&...extra) {
127128
initialize(
128-
std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr, extra...);
129+
std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr, std::forward<Extra>(extra)...);
129130
}
130131

131132
/// Construct a cpp_function from a class method (non-const, no ref-qualifier)

0 commit comments

Comments
 (0)