У версіях 13.x та 16.x clang-tidy виявляє порушення cppcoreguidelines-pro-bounds-array-to-pointer-decay у дивно специфічній ситуації: при ітерації по масиву, до якого звертаються через вказівник. Чому це відбувається? Як range-for може працювати після того, як масив перетворився на вказівник?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int main() { int local[] { 1, 2, 3 }; static int global[] { 1, 2, 3 }; struct Wrapper { int field[3] { 1, 2, 3 }; } wrapper; Wrapper *pointer = &wrapper; int (&reference)[3] = pointer->field; for (int i : local); for (int i : global); for (int i : wrapper.field); for (int i : pointer->field); for (int i : reference); } $ clang-tidy-16 main.cpp --checks='-*,cppcoreguidelines-pro-bounds-array-to-pointer-decay' (...) main.cpp:16:13: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] for (int i : pointer->field); ^ |
Чому лише
1 |
pointer->field |
спричиняє це попередження?