In this specific case, I would suggest using the smaller container's index type; this would be Eigen's index type, as determined by your Eigen::VectorXd
. Ideally, it would be used as Eigen::Index
, for forwards-compatibility.
It might also be worth looking into how Eigen defines its index type. In particular, you are allowed to redefine it if necessary, by changing a preprocessor directive, by #define
ing the symbol EIGEN_DEFAULT_DENSE_INDEX_TYPE
; it defaults to std::ptrdiff_t
.
[Note, however, that in my own code, I generally prefer to use the larger index (in this case, size_t
), but do range checks as if using the smaller of the index types if applicable (in this case, Eigen::Index
). This is just a personal preference, however, and not necessarily what I consider to be the best option.]
Generally, when trying to choose the best index type, I would suggest that you look at their available ranges. First, if one or more of the potential types are signed, and if one or more signed potential type allows negative values*, you'll want to eliminate any unsigned types, especially ones that are larger than the largest signed type. Then, you'd look at your use case, eliminate any types that aren't viable for your intended purpose, and choose the best fit out of the remaining potential types.
In your case specifically, you want to store values from an Eigen3 container in an STL container, where the Eigen3 container is indexed with ptrdiff_t
and (as mentioned in your comment) to your knowledge only uses non-negative index values. In this case, either is a viable option; the range of non-negative index values provided by ptrdiff_t
fits nicely inside size_t
's range, and the loop condition will be determined by your VectorXd
(and thus is also guaranteed to fit inside the Eigen3 container's index type). Thus, both potential types are viable choices. As the additional range provided by size_t
is currently unnecessary, I would consider the index type provided by your Eigen setup to be slightly better suited to the task at hand.
*: While it's typically safe to assume that index values will always be positive due to how indexing works, I can see a few cases where allowing negatives would be beneficial. These are typically rare, though.
Note that I assumed the loop condition i<myStdVector.size()
in your example code was a typo, due to not lining up with the initial description or the operation performed inside the loop body. If I was incorrect, then this decision becomes more complex.