我试过了:
#include <vector>
int main () {
std::vector<int> v;
int size = v.size;
}
Run Code Online (Sandbox Code Playgroud)
但得到了错误:
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'
Run Code Online (Sandbox Code Playgroud)
将表达式转换为int如下:
#include <vector>
int main () {
std::vector<int> v;
int size = (int)v.size;
}
Run Code Online (Sandbox Code Playgroud)
也会产生错误:
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 '()' …Run Code Online (Sandbox Code Playgroud)