-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: master
Are you sure you want to change the base?
Conversation
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: |
There was a problem hiding this comment.
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)))) |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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)))) |
There was a problem hiding this comment.
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.
Delegates to the existing implementation for std::tuple.