4

According to the Eigen documentation, the second and third template arguments, which correspond to number of rows and cols, are expected to be int.

I am wondering why these arguments are not size_t? The only reason to have int there is, when eigen would allow to have negative number of rows or cols?

The only reason why this could make sense is because of Eigen::Dynamic. Can anyone confirm that this is the reason to allow negative values for rows/cols?

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
Simon
  • 706
  • 6
  • 23
  • 1
    Maybe, maybe not - probably you should ask the library authors. I'd say these are `int` because `Eigen::Dynamic` is an `int` that is a negative value with a special meaning in the library. But then again, that doesn't really explain the choice of `int`, they could have easily made it `ssize_t` which is a signed version of `size_t`. – Filipe Gonçalves Nov 30 '15 at 08:17
  • 1
    For large matrices, it is better to use Dynamic than a huge fixed size. Using unsigned types for anything but bitfields or bignum is not recommended. – Marc Glisse Nov 30 '15 at 08:33
  • 1
    @FilipeGonçalves `ssize_t` is not part of c++, but rather POSIX, IIRC, the range being [-1...2*10^15] or something. As Eigen is meant to be cross platform, it doesn't have that option. – Avi Ginsburg Dec 01 '15 at 11:09
  • 1
    @Marc Glisse That is not recommended by whom? Random access to C++ standard library containers is made by size_t indexes as well as their sizes are returned as size_t. This basically makes programs interfacing Eigen with C++ code (apart from toy examples) very warning-emitting or full of casts. – Slava Sep 23 '16 at 13:06
  • @Slava http://stackoverflow.com/q/18795453/1918193 – Marc Glisse Sep 23 '16 at 13:21
  • "Use the appropriate type, and when you don't know, use an int until you do know." - accepted answer. That is very far from "not recommended". Although Bjarne citation was indeed quite strong. – Slava Sep 23 '16 at 13:27

1 Answers1

0

Eigen::Dynamic is not a reason at all as it could be made equal for instance to numeric_limits<size_t>::max(), or same size_t(-1).

I believe that is rather because the target audience of Eigen is more comfortable using int then anything else. Then if you are not comfortable with this choice, you can use the EIGEN_DEFAULT_DENSE_INDEX_TYPE preprocessor directive to set to whatever you feel is appropriate and face the consequences... :) No, seriously, I would at least run the complete Eigen test set before actually using that.

Slava
  • 1,528
  • 1
  • 15
  • 23