更新时间:2021-12-23 02:20:02
std::vector<Foo>::size_type
与std::size_t
相同并不是不必要.即使对于C ++ 11也是如此.
It is not necessarily true that std::vector<Foo>::size_type
is the same as std::size_t
. This is true even for C++11.
但就我个人而言,无论类型如何,我都将std::size_t
用于std::vector
索引.
But personally I use std::size_t
for a std::vector
index irrespective of the type.
如果您特别勤奋,则可以始终使用静态断言.显然,static_assert
是C ++ 98以后的新增内容,但是按照该标准,您可以使用类似
You could always use a static assertion if you're feeling particularly diligent. Obviously static_assert
is a later addition beyond what's in C++98, but in that standard you could use something like
static char wrong_size_1[1 + sizeof(std::size_t) - sizeof(std::vector<Foo>::size_type)];
static char wrong_size_2[1 - sizeof(std::size_t) + sizeof(std::vector<Foo>::size_type)];
如果类型类型的大小不同,则会导致编译时失败.
which would induce compile time failures if type types are not the same size.