I'm in a project which has a strict code style requirement. And the -Werror=convertion
compiling option is mandatory.
When I use the library Eigen, it seems even the basic matrix muliplication will break the 'convertion' rules.
So, what is the 'right' way to eliminate the matrix convertion warns/errors in Eigen?
I have tried force type casting and it work well, but I wonder whether it is a 'gracefull' way. Any other solutions?
The problem can be illustrated by the following test code.
main.cc:
#include <iostream>#include <Eigen/Core>int main(int argc, char* argv[]) { Eigen::Matrix3d m; m << 1, 1, 1, 2, 2, 2, 3, 3, 3; Eigen::Vector3d v1(1, 2, 3); // -Werror=convertion will be triggered: 'Eigen::Type' to 'Eigen::Matrix' Eigen::Vector3d v2 = m * v1; // one solution: force type casting, it's OK // Eigen::Vector3d v2 = (Eigen::Vector3d)(m * v1); std::cout << v2 << std::endl; return 0;}
g++ compile command
g++ main.cc -I../eigen-3.2.10/ -Werror=conversion
compile errors:
In file included from ../eigen-3.2.10/Eigen/Geometry:45:0, from main.cc:9:../eigen-3.2.10/Eigen/src/Geometry/Hyperplane.h:48:69: error: conversion to ‘int’ from ‘Eigen::Hyperplane<_Scalar, _AmbientDim, Options>::Index {aka long int}’ may alter its value [-Werror=conversion] : Index(AmbientDimAtCompileTime)+1,1,Options> Coefficients; ^main.cc: In function ‘int main(int, char**)’:main.cc:60:29: error: choosing ‘Eigen::CoeffBasedProduct<Lhs, Rhs, NestingFlags>::operator const PlainObject&() const [with LhsNested = const Eigen::Matrix<double, 3, 3>&; RhsNested = const Eigen::Matrix<double, 3, 1>&; int NestingFlags = 6; Eigen::CoeffBasedProduct<Lhs, Rhs, NestingFlags>::PlainObject = Eigen::Matrix<double, 3, 1>]’ over ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::CoeffBasedProduct<const Eigen::Matrix<double, 3, 3>&, const Eigen::Matrix<double, 3, 1>&, 6>; _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’ [-Werror=conversion] Eigen::Vector3d v2 = m3 * v1; ^~main.cc:60:29: error: for conversion from ‘const Type {aka const Eigen::CoeffBasedProduct<const Eigen::Matrix<double, 3, 3>&, const Eigen::Matrix<double, 3, 1>&, 6>}’ to ‘Eigen::Vector3d {aka Eigen::Matrix<double, 3, 1>}’ [-Werror=conversion]main.cc:60:29: note: because conversion sequence for the argument is bettermain.cc:60:29: error: choosing ‘Eigen::CoeffBasedProduct<Lhs, Rhs, NestingFlags>::operator const PlainObject&() const [with LhsNested = const Eigen::Matrix<double, 3, 3>&; RhsNested = const Eigen::Matrix<double, 3, 1>&; int NestingFlags = 6; Eigen::CoeffBasedProduct<Lhs, Rhs, NestingFlags>::PlainObject = Eigen::Matrix<double, 3, 1>]’ over ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::EigenBase<OtherDerived>&) [with OtherDerived = Eigen::CoeffBasedProduct<const Eigen::Matrix<double, 3, 3>&, const Eigen::Matrix<double, 3, 1>&, 6>; _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’ [-Werror=conversion]main.cc:60:29: error: for conversion from ‘const Type {aka const Eigen::CoeffBasedProduct<const Eigen::Matrix<double, 3, 3>&, const Eigen::Matrix<double, 3, 1>&, 6>}’ to ‘Eigen::Vector3d {aka Eigen::Matrix<double, 3, 1>}’ [-Werror=conversion]main.cc:60:29: note: because conversion sequence for the argument is bettercc1plus: some warnings being treated as errors