Skip to content

Commit

Permalink
More perfect forward
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyever committed Sep 30, 2024
1 parent f9cc695 commit eb2660b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
28 changes: 14 additions & 14 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,22 @@ void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
template <typename... Args>
struct constructor {
template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
static void execute(Class &cl, const Extra &...extra) {
static void execute(Class &cl, Extra &&...extra) {
cl.def(
"__init__",
[](value_and_holder &v_h, Args... args) {
v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
},
is_new_style_constructor(),
extra...);
std::forward<Extra>(extra)...);
}

template <
typename Class,
typename... Extra,
enable_if_t<Class::has_alias && std::is_constructible<Cpp<Class>, Args...>::value, int>
= 0>
static void execute(Class &cl, const Extra &...extra) {
static void execute(Class &cl, Extra &&...extra) {
cl.def(
"__init__",
[](value_and_holder &v_h, Args... args) {
Expand All @@ -229,23 +229,23 @@ struct constructor {
}
},
is_new_style_constructor(),
extra...);
std::forward<Extra>(extra)...);
}

template <
typename Class,
typename... Extra,
enable_if_t<Class::has_alias && !std::is_constructible<Cpp<Class>, Args...>::value, int>
= 0>
static void execute(Class &cl, const Extra &...extra) {
static void execute(Class &cl, Extra &&...extra) {
cl.def(
"__init__",
[](value_and_holder &v_h, Args... args) {
v_h.value_ptr()
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
},
is_new_style_constructor(),
extra...);
std::forward<Extra>(extra)...);
}
};

Expand All @@ -257,15 +257,15 @@ struct alias_constructor {
typename... Extra,
enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int>
= 0>
static void execute(Class &cl, const Extra &...extra) {
static void execute(Class &cl, Extra &&...extra) {
cl.def(
"__init__",
[](value_and_holder &v_h, Args... args) {
v_h.value_ptr()
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
},
is_new_style_constructor(),
extra...);
std::forward<Extra>(extra)...);
}
};

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

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

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions include/pybind11/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ template <op_id id, op_type ot, typename L, typename R>
struct op_ {
static constexpr bool op_enable_if_hook = true;
template <typename Class, typename... Extra>
void execute(Class &cl, const Extra &...extra) const {
void execute(Class &cl, Extra &&...extra) const {
using Base = typename Class::type;
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
using op = op_impl<id, ot, Base, L_type, R_type>;
cl.def(op::name(), &op::execute, is_operator(), extra...);
cl.def(op::name(), &op::execute, is_operator(), std::forward<Extra>(extra)...);
}
template <typename Class, typename... Extra>
void execute_cast(Class &cl, const Extra &...extra) const {
void execute_cast(Class &cl, Extra &&...extra) const {
using Base = typename Class::type;
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
using op = op_impl<id, ot, Base, L_type, R_type>;
cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
cl.def(op::name(), &op::execute_cast, is_operator(), std::forward<Extra>(extra)...);
}
};

Expand Down
5 changes: 3 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class cpp_function : public function {
// NOLINTNEXTLINE(google-explicit-constructor)
cpp_function(std::nullptr_t) {}
cpp_function(std::nullptr_t, const is_setter &) {}
cpp_function(std::nullptr_t, is_setter &&) {}

/// Construct a cpp_function from a vanilla function pointer
template <typename Return, typename... Args, typename... Extra>
Expand All @@ -123,9 +124,9 @@ class cpp_function : public function {
typename... Extra,
typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
// NOLINTNEXTLINE(google-explicit-constructor)
cpp_function(Func &&f, const Extra &...extra) {
cpp_function(Func &&f, Extra &&...extra) {
initialize(
std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr, extra...);
std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr, std::forward<Extra>(extra)...);
}

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

0 comments on commit eb2660b

Please sign in to comment.