bugprone-sizeof-container

此检查会查找对 STL 容器类型表达式使用 sizeof 的情况。用户很可能想使用 .size()

所有在命名空间 std:: 中声明的类/结构类型,如果拥有一个常量 size() 方法,则被视为容器,std::bitsetstd::array 除外。

示例

std::string s;
int a = 47 + sizeof(s); // warning: sizeof() doesn't return the size of the container. Did you mean .size()?

int b = sizeof(std::string); // no warning, probably intended.

std::string array_of_strings[10];
int c = sizeof(array_of_strings) / sizeof(array_of_strings[0]); // no warning, definitely intended.

std::array<int, 3> std_array;
int d = sizeof(std_array); // no warning, probably intended.