Add support for Eigen::Ref<...> function arguments #312
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.
Eigen::Ref is a common way to pass eigen dense types without needing a template, e.g. the single definition
void func(const Eigen::Ref<const Eigen::MatrixXd> &x)
is a useful signature to accept any double-coefficient matrix-like object/block/etc. (without needing the implementation in a template.)The current pybind11 eigen support fails with internal errors if attempting to bind a function with an Eigen::Ref<...> argument because, while Eigen::Ref<...> satisfies the "is_eigen_dense" requirement, it can't compile if used: Eigen::Ref<...> itself is not default constructible, and so the argument loader can't default construct an std::tuple containing an Eigen::Ref<...>, which results in compilation failure.
This commit adds support for Eigen::Ref<...> arguments by giving it its own type_caster implementation which basically is just a wrapper around the type_caster for the referenced type with a unique_ptr storing the required Ref object (which itself references the wrapped type_caster).
There is, of course, no performance advantage for pybind11-using code of using Eigen::Ref<...>--we are allocating a matrix of the derived type when loading it--this is mainly about allowing pybind11 to bind transparently to C++ methods taking Eigen::Refs.