Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use perfect forward #5388

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), pos_only());

#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
Loading
Loading