I tried:
#include <vector>
int main () {
std::vector<int> v;
int size = v.size;
}
but got the error:
cannot convert 'std::vector<int>::size' from type 'std::vector<int>::size_type (std::vector<int>::)() const noexcept' {aka 'long unsigned int (std::vector<int>::)() const noexcept'} to type 'int'
Casting the expression to int
like this:
#include <vector>
int main () {
std::vector<int> v;
int size = (int)v.size;
}
also yields an error:
error: invalid use of member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' (did you forget the '()' ?)
Last I tried:
#include <vector>
int main () {
std::vector<int> v;
int size = v.size();
}
which gave me:
warning: implicit conversion loses integer precision
How can I fix this?
std::size(v);
it is introduced with c++17 – Repeatstd::ssize(v);
that is introduced with C++20 becausestd::size
doesn't fix the last problem. – Camberstd::size
returns an unsigned integer,std::ssize
returns a signed integer. Except I just noticed thatssize
doesn't solve the problem either ... – Camber