1. 求数组长度
求数组长度一般采用的是#define count_of(arg) (sizeof(arg) / sizeof(arg[0])),但这个宏对于指针在编译是不报错的,chrome里面是这么干的:
template char (&ArraySizeHelper(T (&array)[N]))[N]; #define arraysize(array) ( sizeof(ArraySizeHelper(array))) void Test( int C[3]) { int A[3]; int *B = Foo(); size_t x = arraysize(A); // Ok x = arraysize(B); // Compilation error x = arraysize(C); // Compilation error }
2. 删除文件
文件删除在某些时候是会失败的,再等待一段时间就可以删除了。
if (!file_util::Delete(db_name, false) && !file_util::Delete(db_name, false)) { // Try to delete twice. If we can't, fail. LOG(ERROR) << "unable to delete old TopSites file"; return false; }
来源于PVS-Studio vs Chromium, 这篇文章主要是讲PVS-Studio分析chrome代码。