std::vector<int> myVec;
long nVecSize = (long)myVec.size(); // no warning
当您确定这样做是安全的时,请将 64 位传入值替换为 32 位值,如下所示:
extern long myGetSize(const std::vector<int> & ); // applies the truncation hard cast internally
std::vector<int> myVec;
long nVecSize = myGetSize(myVec );
暂时禁用编译器警告,然后重新启用它,如下所示:
std::vector<int> myVec;
// we know size is < 4G, so is okay to ignore warning
#pragma warning (push)
#pragma warning (disable: 4267)
long nVecSize = myGetSize(myVec);
#pragma warning (pop)