mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-03-13 18:37:27 +08:00
problem solved, we really want public inheritance and it is only
automatic when the _child_ class is a struct.
This commit is contained in:
parent
1b88042880
commit
8551505436
@ -132,7 +132,7 @@ class Matrix
|
||||
protected:
|
||||
ei_matrix_storage<Scalar, MaxSizeAtCompileTime, RowsAtCompileTime, ColsAtCompileTime, Options> m_storage;
|
||||
|
||||
public: // FIXME should this be public? I'd say yes but I still don't understand then why at other places we've been having private new and delete operators.
|
||||
public:
|
||||
enum { NeedsToAlign = (Options&Matrix_AutoAlign) == Matrix_AutoAlign
|
||||
&& SizeAtCompileTime!=Dynamic && ((sizeof(Scalar)*SizeAtCompileTime)%16)==0 };
|
||||
typedef typename ei_meta_if<NeedsToAlign, ei_byte_forcing_aligned_malloc, char>::ret ByteAlignedAsNeeded;
|
||||
|
@ -173,7 +173,7 @@ inline static int ei_alignmentOffset(const Scalar* ptr, int maxOffset)
|
||||
* overloading the operator new to return aligned data when the vectorization is enabled.
|
||||
* Here is a similar safe example:
|
||||
* \code
|
||||
* struct Foo : WithAlignedOperatorNew {
|
||||
* struct Foo : public WithAlignedOperatorNew {
|
||||
* char dummy;
|
||||
* Vector4f some_vector;
|
||||
* };
|
||||
@ -201,7 +201,7 @@ struct WithAlignedOperatorNew
|
||||
|
||||
template<typename T, int SizeAtCompileTime,
|
||||
bool NeedsToAlign = (SizeAtCompileTime!=Dynamic) && ((sizeof(T)*SizeAtCompileTime)%16==0)>
|
||||
struct ei_with_aligned_operator_new : WithAlignedOperatorNew {};
|
||||
struct ei_with_aligned_operator_new : public WithAlignedOperatorNew {};
|
||||
|
||||
template<typename T, int SizeAtCompileTime>
|
||||
struct ei_with_aligned_operator_new<T,SizeAtCompileTime,false> {};
|
||||
|
@ -16,7 +16,7 @@ namespace Eigen {
|
||||
If you saw the assertion failure that links to this page, then you probably have done something like that in your code:
|
||||
|
||||
\code
|
||||
struct Foo
|
||||
class Foo
|
||||
{
|
||||
...
|
||||
Eigen::Vector2d v;
|
||||
@ -28,7 +28,7 @@ struct Foo
|
||||
Foo *foo = new Foo;
|
||||
\endcode
|
||||
|
||||
In other words: you have probably in your code a structure that has as a member a vectorizable fixed-size Eigen object, and you then dynamically allocated an object of that structure.
|
||||
In other words: you have probably in your code a class that has as a member a vectorizable fixed-size Eigen object, and you then dynamically allocated an object of that class.
|
||||
|
||||
By "vectorizable fixed-size Eigen object" we mean an Eigen matrix or vector of fixed size, and whose size is a multiple of 128 bits. Examples include:
|
||||
\li Eigen::Vector2d
|
||||
@ -43,10 +43,10 @@ By "vectorizable fixed-size Eigen object" we mean an Eigen matrix or vector of f
|
||||
|
||||
\section how How to fix this bug?
|
||||
|
||||
Very easy, you just need to let your struct Foo inherit Eigen::WithAlignedOperatorNew, like this:
|
||||
Very easy, you just need to let your class Foo publicly inherit Eigen::WithAlignedOperatorNew, like this:
|
||||
|
||||
\code
|
||||
struct Foo : Eigen::WithAlignedOperatorNew
|
||||
class Foo : public Eigen::WithAlignedOperatorNew
|
||||
{
|
||||
...
|
||||
Eigen::Vector2d v;
|
||||
@ -65,7 +65,7 @@ With this, you should be out of trouble.
|
||||
OK let's say that your code looks like this:
|
||||
|
||||
\code
|
||||
struct Foo
|
||||
class Foo
|
||||
{
|
||||
...
|
||||
Eigen::Vector2d v;
|
||||
@ -85,18 +85,18 @@ For this reason, Eigen takes care by itself to require 128-bit alignment for Eig
|
||||
|
||||
Thus, normally, you don't have to worry about anything, Eigen handles alignment for you...
|
||||
|
||||
... except in one case. When you have a struct Foo like above, and you dynamically allocate a new Foo as above, then, since Foo doesn't have aligned "operator new", the returned pointer foo is not necessarily 128-bit aligned.
|
||||
... except in one case. When you have a class Foo like above, and you dynamically allocate a new Foo as above, then, since Foo doesn't have aligned "operator new", the returned pointer foo is not necessarily 128-bit aligned.
|
||||
|
||||
The alignment attribute of the member v is then relative to the start of the struct, foo. If the foo pointer wasn't aligned, then foo->v won't be aligned either!
|
||||
The alignment attribute of the member v is then relative to the start of the class, foo. If the foo pointer wasn't aligned, then foo->v won't be aligned either!
|
||||
|
||||
The solution is to let struct Foo have an aligned "operator new", as we showed in the previous section.
|
||||
The solution is to let class Foo have an aligned "operator new", as we showed in the previous section.
|
||||
|
||||
\section movetotop Should I then put all the members of Eigen types at the beginning of my struct?
|
||||
\section movetotop Should I then put all the members of Eigen types at the beginning of my class?
|
||||
|
||||
No, that's not needed. Since Eigen takes care of declaring 128-bit alignment, all members that need it are automatically 128-bit aligned relatively to the struct. So when you have code like
|
||||
No, that's not needed. Since Eigen takes care of declaring 128-bit alignment, all members that need it are automatically 128-bit aligned relatively to the class. So when you have code like
|
||||
|
||||
\code
|
||||
struct Foo : Eigen::WithAlignedOperatorNew
|
||||
class Foo : public Eigen::WithAlignedOperatorNew
|
||||
{
|
||||
double x;
|
||||
Eigen::Vector2d v;
|
||||
@ -106,7 +106,7 @@ struct Foo : Eigen::WithAlignedOperatorNew
|
||||
it will work just fine. You do \b not need to rewrite it as
|
||||
|
||||
\code
|
||||
struct Foo : Eigen::WithAlignedOperatorNew
|
||||
class Foo : public Eigen::WithAlignedOperatorNew
|
||||
{
|
||||
Eigen::Vector2d v;
|
||||
double x;
|
||||
|
Loading…
x
Reference in New Issue
Block a user