Reply to topic

Failure matching MatrixBase

mikep
Registered Member
Posts
2
Karma
0

Failure matching MatrixBase

Thu Apr 11, 2013 9:52 pm
I'm somewhat new to Eigen and trying to make a C++11 coherent noise library using Eigen, but I've hit a problem. I can't for the life of me figure out why the following code can't compile. I've followed the "Writing Functions Taking Eigen Types as Parameters" guide, but using the MatrixBase Parameter just makes it fail during compilation.
Code: Select all
#include <Eigen/Dense>

//Applies a offset before calling a function
template <typename Function, typename Scalar, int dimensions>
class offset_functor
{
   typedef typename Eigen::Matrix<Scalar, dimensions, 1> Offset;
   Function function;
   Offset offset;

   public:
   offset_functor(Function _function, Offset _offset)
      : function(_function), offset(_offset)
   { }

   template <typename Derived>
   auto operator()(const Eigen::MatrixBase<Derived>& position)
      -> decltype(function(offset + position))
   {
      function(offset + position);
   }
};

//Constructs a offset_functor (lets us use type inference)
template <typename Function, typename Scalar, int dimensions>
offset_functor<Function, Scalar, dimensions> make_offset_functor(Function _function, Eigen::Matrix<Scalar, dimensions, 1> _offset)
{
   return offset_functor<Function, Scalar, dimensions>(_function, _offset);
}

//This function fails, while the "g" function doesn't.
//It works if make the parameter Derived instead
//(i.e. making the entire parameter generic, but that just seems like a bad code smell).
template <typename Derived>
void f(const Eigen::MatrixBase<Derived>&)
{ }

void g(const Eigen::Vector2f&)
{ }

int main()
{
   //This fails to compile (no match for the offset_functor's operator())
   auto a = make_offset_functor(f<Eigen::Vector2f>, Eigen::Vector2f(0,0));
   a(Eigen::Vector2f(0,0));
   
   //This compiles ok
   auto b = make_offset_functor(g, Eigen::Vector2f(0,0));
   b(Eigen::Vector2f(0,0));
}
User avatar ggael
Moderator
Posts
2195
Karma
15
OS

Re: Failure matching MatrixBase

Fri Apr 12, 2013 9:05 am
This is because f<Eigen::Vector2f> will only accept objects deriving from: MatrixBase<Vector2f> while you are calling it with an object representing the addition of two vectors.
User avatar ggael
Moderator
Posts
2195
Karma
15
OS

Re: Failure matching MatrixBase

Fri Apr 12, 2013 9:06 am
I don't know what you are willing to achieve, but maybe one solution would be to use for f a true functor having a templated operator().
mikep
Registered Member
Posts
2
Karma
0

Re: Failure matching MatrixBase

Fri Apr 12, 2013 1:21 pm
Got it working with a function object with a templated operator() like you suggested. Thanks. :)

 
Reply to topic

Bookmarks



Who is online

Registered users: alexweb, Baidu [Spider], Bing [Bot], dgraf, Exabot [Bot], ggael, Google [Bot], GreatEmerald, ken300, koriun, lazyit, netpalantir, numbchild, paulus3005, pbCyanide, rulet111, Tsukasa Buddha, valoriez, vascobasque, wolfi323, Yahoo [Bot], Zagupi