This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Multiple Inheritance and Eigen Alignment

Tags: None
(comma "," separated)
xerion
Registered Member
Posts
20
Karma
0
Hi all,

I was wondering if anyone has used Eigen with fixed size matrices and multiple inheritance as I am facing some errors due to the multiple definition of EIGEN_MAKE_ALIGNED_OPERATOR_NEW;

Code: Select all

class Foo1
{
public:
  Foo1() = default;
  virtual ~Foo1() = default;

  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
protected:
  Eigen::Matrix<double, 3, 1> parameters_;
};

class Foo2
{
public:
  Foo2() = default;
  virtual ~Foo2() = default;

  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
protected:
  Eigen::Matrix<double, 6, 1> parameters_;
};

class Derived: public Foo1, public Foo2
{
public:
  Derived() = default;
  ~Derived() = default;

  //EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(false)
  //EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};


error C2385: ambiguous access of 'delete'
2> could be the 'delete' in base 'Foo1'
2> or could be the 'delete' in base 'Foo2'
2> This diagnostic occurred in the compiler generated function 'void *Derived::__delDtor(unsigned int)'

One solution I guess is to hide the fixed size matrices in a struct as per the documentation but it becomes a bit cumbersome for the derived classes and requires a heap allocation.

However, do we really need to align in derived class ?
Would a solution be to actually add EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(false)

If not would there be any issues if we just redefine EIGEN_MAKE_ALIGNED_OPERATOR_NEW in the Derived classes so that we hide the inherited functions ?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
xerion wrote:redefine EIGEN_MAKE_ALIGNED_OPERATOR_NEW in the Derived classes so that we hide the inherited functions ?


yes, this is the right solution.
xerion
Registered Member
Posts
20
Karma
0
Thank you for your answer.

Could you provide some explanation though ?
I would have thought that since Derived does not have any fixed size matrices as members it would not need the EIGEN_MAKE_ALIGNED_OPERATOR_NEW.
The base classes that have the fixed matrices also have the macro so that should take care of their initialization.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Since Derived inherits Foo1, any Derived instance has to be properly allocated to preserve the alignement requirement of Foo1. In the case of single inheritance, like:

class Derived: public Foo1 {};

it is not needed to redefine operator new in Derived because it is automatically inherited from Foo1. More precisely, doing new Derived; will call Foo1::operator new that will call Eigen's aligned memory allocator to allocate the entire Derived object, that is a buffer of size sizeof(Derived). Redefining operator new in Derived using Eigen's macro is therefore harmless.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]