Transformation history in MatrixBase <Derived>

What happens when you pass a matrix object to a function as a MatrixBase reference? I don’t understand what is really going on behind the scenes.

Example function code:

#include <Eigen/Core>
#include <iostream>

using namspace Eigen;

template <typename Derived>
void print_size(const MatrixBase<Derived>& b)
{
  std::cout << "size (rows, cols): " << b.size() << " (" << b.rows()
            << ", " << b.cols() << ")" << std::endl;
  std::cout << sizeof(b) << std::endl;
}

int main() {
    Matrix<float, 2, 2> m;
    m << 0.0, 0.1,
         0.2, 0.3;

    print_size(m);
    std::cout << sizeof(m) << std::endl;
}

It gives the following result:

size (rows, cols): 4 (2, 2)
1
16

Where does the difference 16 versus 1 come from?

And why do you need a conversion?

Thanks in advance!

+4
source share
2 answers

sizeofevaluated at compile time, so it is associated with a declared (static) type of object. bhas a type MatrixBase<Derived>(ignoring the link as it does sizeof), which is most likely an empty base class and therefore has a size of 1.

m, , Matrix<float, 2, 2>, , -, 16 .

, sizeof.

+7

sizeof . . sizeof

sizeof , , .

, MatrixBase<Derived> .

sizeof(b) , sizeof(MatrixBase<Derived>).

+3

Source: https://habr.com/ru/post/1692643/


All Articles