I am trying to use the GMP number library together with Eigen matrix library. I try to instantiate the template:
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
with
Matrix<mpz_class, 3, 3> matrix;
where mpz_class is a number class from the GMP library.
I get a compiler error:
/usr/include/eigen3/Eigen/src/Core/MathFunctions.h:409: error:
invalid static_cast from
type ‘const __gmp_expr<__mpz_struct [1], __mpz_struct [1]>’
to type ‘int’
When I examine the source code of the Eigen library, I find out, that the problem is that
mpz_class cannot be static_cast -ed to int in this template:
template<typename OldType, typename NewType>
struct cast_impl
{
static inline NewType run(const OldType& x)
{
return static_cast<NewType>(x);
}
};
How can I bypass this problem? I know how to convert mpz_class to int in runtime, but it must be done by the compiler, since static_cast is compile time.
Assuming
mpz_classis safe to subclass, you can just use a subclass and write a conversion operator: