More perfect forward

This commit is contained in:
cyy 2024-09-30 10:20:44 +08:00
parent f9cc695ad3
commit eb2660b08b
3 changed files with 21 additions and 20 deletions

View File

@ -201,14 +201,14 @@ void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
template <typename... Args> template <typename... Args>
struct constructor { struct constructor {
template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0> 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( cl.def(
"__init__", "__init__",
[](value_and_holder &v_h, Args... args) { [](value_and_holder &v_h, Args... args) {
v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...); v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
template < template <
@ -216,7 +216,7 @@ struct constructor {
typename... Extra, typename... Extra,
enable_if_t<Class::has_alias && std::is_constructible<Cpp<Class>, Args...>::value, int> enable_if_t<Class::has_alias && std::is_constructible<Cpp<Class>, Args...>::value, int>
= 0> = 0>
static void execute(Class &cl, const Extra &...extra) { static void execute(Class &cl, Extra &&...extra) {
cl.def( cl.def(
"__init__", "__init__",
[](value_and_holder &v_h, Args... args) { [](value_and_holder &v_h, Args... args) {
@ -229,7 +229,7 @@ struct constructor {
} }
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
template < template <
@ -237,7 +237,7 @@ struct constructor {
typename... Extra, typename... Extra,
enable_if_t<Class::has_alias && !std::is_constructible<Cpp<Class>, Args...>::value, int> enable_if_t<Class::has_alias && !std::is_constructible<Cpp<Class>, Args...>::value, int>
= 0> = 0>
static void execute(Class &cl, const Extra &...extra) { static void execute(Class &cl, Extra &&...extra) {
cl.def( cl.def(
"__init__", "__init__",
[](value_and_holder &v_h, Args... args) { [](value_and_holder &v_h, Args... args) {
@ -245,7 +245,7 @@ struct constructor {
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...); = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
}; };
@ -257,7 +257,7 @@ struct alias_constructor {
typename... Extra, typename... Extra,
enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int> enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int>
= 0> = 0>
static void execute(Class &cl, const Extra &...extra) { static void execute(Class &cl, Extra &&...extra) {
cl.def( cl.def(
"__init__", "__init__",
[](value_and_holder &v_h, Args... args) { [](value_and_holder &v_h, Args... args) {
@ -265,7 +265,7 @@ struct alias_constructor {
= construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...); = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
}; };
@ -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 // 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. // instance, or the alias needs to be constructible from a `Class &&` argument.
template <typename Class, typename... Extra> template <typename Class, typename... Extra>
void execute(Class &cl, const Extra &...extra) && { void execute(Class &cl, Extra &&...extra) && {
#if defined(PYBIND11_CPP14) #if defined(PYBIND11_CPP14)
cl.def( cl.def(
"__init__", "__init__",
@ -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); v_h, func(std::forward<Args>(args)...), Py_TYPE(v_h.inst) != v_h.type->type);
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
}; };
@ -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 // 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. // class (i.e. not inherited), the alias factory when `self` is a Python-side subtype.
template <typename Class, typename... Extra> template <typename Class, typename... Extra>
void execute(Class &cl, const Extra &...extra) && { void execute(Class &cl, Extra &&...extra) && {
static_assert(Class::has_alias, static_assert(Class::has_alias,
"The two-argument version of `py::init()` can " "The two-argument version of `py::init()` can "
"only be used if the class has an alias"); "only be used if the class has an alias");
@ -359,7 +359,7 @@ struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
} }
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
}; };
@ -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)) {} pickle_factory(Get get, Set set) : get(std::forward<Get>(get)), set(std::forward<Set>(set)) {}
template <typename Class, typename... Extra> template <typename Class, typename... Extra>
void execute(Class &cl, const Extra &...extra) && { void execute(Class &cl, Extra &&...extra) && {
cl.def("__getstate__", std::move(get)); cl.def("__getstate__", std::move(get));
#if defined(PYBIND11_CPP14) #if defined(PYBIND11_CPP14)
@ -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); v_h, func(std::forward<ArgState>(state)), Py_TYPE(v_h.inst) != v_h.type->type);
}, },
is_new_style_constructor(), is_new_style_constructor(),
extra...); std::forward<Extra>(extra)...);
} }
}; };

View File

@ -86,20 +86,20 @@ template <op_id id, op_type ot, typename L, typename R>
struct op_ { struct op_ {
static constexpr bool op_enable_if_hook = true; static constexpr bool op_enable_if_hook = true;
template <typename Class, typename... Extra> 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 Base = typename Class::type;
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>; 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 R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
using op = op_impl<id, ot, Base, L_type, R_type>; 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> 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 Base = typename Class::type;
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>; 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 R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
using op = op_impl<id, ot, Base, L_type, R_type>; 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)...);
} }
}; };

View File

@ -110,6 +110,7 @@ public:
// NOLINTNEXTLINE(google-explicit-constructor) // NOLINTNEXTLINE(google-explicit-constructor)
cpp_function(std::nullptr_t) {} cpp_function(std::nullptr_t) {}
cpp_function(std::nullptr_t, const is_setter &) {} cpp_function(std::nullptr_t, const is_setter &) {}
cpp_function(std::nullptr_t, is_setter &&) {}
/// Construct a cpp_function from a vanilla function pointer /// Construct a cpp_function from a vanilla function pointer
template <typename Return, typename... Args, typename... Extra> template <typename Return, typename... Args, typename... Extra>
@ -123,9 +124,9 @@ public:
typename... Extra, typename... Extra,
typename = detail::enable_if_t<detail::is_lambda<Func>::value>> typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
// NOLINTNEXTLINE(google-explicit-constructor) // NOLINTNEXTLINE(google-explicit-constructor)
cpp_function(Func &&f, const Extra &...extra) { cpp_function(Func &&f, Extra &&...extra) {
initialize( 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) /// Construct a cpp_function from a class method (non-const, no ref-qualifier)