Accept any sequence type as std::tuple or std::pair #534
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was touched on briefly in PR #445. Similar to that PR, this makes the tuple caster more Pythonic by accepting any sequence.
The only drawback is that this makes overloading on
std::tuple
andstd::vector
ambiguous, but shouldn't be a problem in practice.std::vector
andstd::list
are already ambiguous overloads and it doesn't seem to have been an issue so far. I don't think there's a lot of sense in exporting a function overloaded onstd::tuple
andstd::vector
to Python -- it's not very Pythonic to expect different results fromfoo([1, 2, 3])
andfoo((1, 2, 3))
.Regarding the implementation, the tuple caster and function argument loader are split into two different classes. This is needed in order to allow the tuple caster to accept any sequence while keeping the argument loader fast. There is very little overlap between the two classes which makes the separation clean. It’s also good practice not to have completely new functionality in a specialization.
The last commit slightly tweaks
make_index_sequence
to make it conform to thestd
version. That way, in C++14 mode, the standard library version can be used which can be a big advantage on newer compilers which make these sequences free, thus removing the possibility of hitting recursive template instantiation limits.