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

ENH: Add to and from object support for std::pair. #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ssanderson
Copy link

Delegates to the existing implementation for std::tuple.

Delegates to the existing implementation for std::tuple.
@@ -356,6 +357,15 @@ struct from_object<std::tuple<Ts...>> {
}
};

template<typename T, typename U>
struct from_object<std::pair<T, U>> {
public:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can drop the publics from these structs.

public:
static PyObject* f(const std::pair<T, U>& p) {
// Delegate to std::tuple dispatch.
return std::move(py::to_object(std::make_tuple(std::get<0>(p), std::get<1>(p))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will already be an rvalue, so we don't need to move

public:
static std::pair<T, U> f(PyObject* tup) {
std::tuple<T, U> tmp = from_object<std::tuple<T, U>>(tup);
return std::make_pair(std::get<0>(tmp), std::get<1>(tmp));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move the values from the old tuple in the resulting pair. For things like vector and string this will trigger a copy.

public:
static PyObject* f(const std::pair<T, U>& p) {
// Delegate to std::tuple dispatch.
return std::move(py::to_object(std::make_tuple(std::get<0>(p), std::get<1>(p))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is triggering an extra copy. Should we just pull the tuple to_object into a helper base class that accepts tuple-likes and subclass from that? This is how we handle mappings and sequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants