Here it is written that std::ranges::size
should return an unsigned integer. However, when I use it on an Eigen vector (with Eigen 3.4) the following compiles:
Eigen::VectorXd x;
static_assert(std::same_as<Eigen::VectorXd::Index,
decltype(std::ranges::size(x))>);
where Eigen::VectorXd::Index
is notoriously a signed integer. By looking at the implementation of std::ranges::size
, I noticed that the return type is inferred from the return type of x.size()
, which is precisely Eigen::VectorXd::Index
. Is this a bug of std::ranges::size
? Or is this expected?
Update 27/12/2021
The C++ reference page linked above has eventually changed the description of the std::ranges::size
function: it only returns an integer, not necessarily an unsigned one!
size()
method is supposed to return an unsigned integer. The problem is at the Eigen's side. – PapiamentoOtherwise, t.size() converted to its decayed type, if ranges::disable_sized_range<std::remove_cv_t<T>> is false, and the converted expression is valid and has an integer-like type.
Looks like underspecified for containers which return signed types for sizes. – Thurnausize()
functions is involved, it returns an unsigned integer (or integer-class) value. Otherwise, it returns the return value of thatsize()
function. – Thuythuya