Reputation: 13
I am trying to solve some problems with Eigen. In the process, I found that static_cast and user-defined conversion conflict, maybe the problem caused by std::enable_if?
Here is basically what I tried:
#include <Eigen/Dense>
#include <bits/stdc++.h>
using namespace std;
class Vector2
{
public:
operator Eigen::Vector2d ()
{
return data;
}
private:
Eigen::Vector2d data;
};
class Box2
{
public:
void Extend(const Vector2 & a)
{
data.extend( static_cast<Eigen::Vector2d>(a) ); // error, but why?
}
private:
Eigen::AlignedBox2d data;
};
int main()
{
Vector2 a;
Eigen::Vector2d b = a; // ok, implicit conversion
return 0;
}
g++ -std=c++11
outputs a lot of error logs, this is the last part:
eigen/eigen/Eigen/src/Core/PlainObjectBase.h:863:30: note: template argument deduction/substitution failed:
eigen/eigen/Eigen/src/Core/PlainObjectBase.h: In substitution of ‘template<class T> void Eigen::PlainObjectBase<Derived>::_init1(const Index&, typename Eigen::internal::enable_if<((((((! Eigen::internal::is_same<long int, typename Eigen::internal::traits<T>::Scalar>::value) && Eigen::internal::is_same<long int, T>::value) && (typename Eigen::internal::dense_xpr_base<Derived>::type:: SizeAtCompileTime != Eigen::Dynamic)) && (typename Eigen::internal::dense_xpr_base<Derived>::type:: SizeAtCompileTime != 1)) && Eigen::internal::is_convertible<T, typename Eigen::internal::traits<T>::Scalar>::value) && Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::ArrayXpr>::value), T*>::type*) [with T = T; Derived = Eigen::Matrix<double, 2, 1>] [with T = Vector2]’:
eigen/eigen/Eigen/src/Core/Matrix.h:296:33: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = Vector2; _Scalar = double; int _Rows = 2; int _Cols = 1; int _Options = 0; int _MaxRows = 2; int _MaxCols = 1]’
test.cpp:21:52: required from here
eigen/eigen/Eigen/src/Core/PlainObjectBase.h:863:30: error: invalid use of incomplete type ‘struct Eigen::internal::enable_if<false, Vector2*>’
In file included from eigen/eigen/Eigen/Core:364:0,
from eigen/eigen/Eigen/Dense:1,
from eigen/eigen/Eigen/Eigen:1,
from test.cpp:2:
eigen/eigen/Eigen/src/Core/util/Meta.h:162:50: error: declaration of ‘struct Eigen::internal::enable_if<false, Vector2*>’
template<bool Condition, typename T=void> struct enable_if;
^
It seems like the issue of enable_if, I wonder why static_cast doesn't work. Is there a way to solve this problem without modifying Eigen? It is ok to do this now:
void Extend(Vector2 a)
{
Eigen::Vector2d b = a;
data.extend( b );
}
But it's too ugly.
Upvotes: 1
Views: 540
Reputation: 5710
It seems like the issue of
enable_if
, I wonder whystatic_cast
doesn't work.
This has nothing to do with the static_cast
itself. The error is generated because Vector2
's operator Eigen::Vector2d ()
isn't defined for const
objects and since your Box2::Extend()
takes a const Vector2& a
, the appropriate casting isn't defined for that line in the code. Add a const
version of Vector2
's user defined casting to Eigen::Vector2d
to make it both compile and keep the const-correctness of the code:
const operator Eigen::Vector2d () const
{
return data;
}
Now the usage is working like you wanted:
int main()
{
Vector2 a;
Eigen::Vector2d b = a; // ok, implicit conversion
Box2 boxy;
boxy.Extend(a); // also ok, implicit conversion inside, from a const&
return 0;
}
Upvotes: 4