4 ms·
heh not really. more like "I'm disappointed that gcc/clang doesn't even give me the option to warn me from doing something that I will surely never want to do,
by 2bitencryption 10y ago
heh not really. more like "I'm disappointed that gcc/clang doesn't even give me the option to warn me from doing something that I will surely never want to do, even if it is "legal" (but undefined) C++, for the sake of safety."
- voyou 10y agoPointer arithmetic isn't "something that I will surely never want to do"; it's something you want to do all the time in C (and therefore in C++ written in a more C-like style). Illegitimate uses of pointer arithmetic, such as your example, are only easy to detect in trivial cases like yours. I assume this is why no one has bothered implementing a warning for this special case. In C++, directly using pointers to access arrays is often discouraged, in favour of using the standard collection types and their associated iterator types. So in code that follows this style, it is possible to warn about uses of pointer arithmetic, and the core C++ guidelines do suggest such a warning. But the guidelines also point out that "this rule would generate a huge number of false positives if applied to an older code base": https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r2-in-interfaces-use-raw-pointers-to-denote-individual-objects-only https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...
- 2bitencryption 10y agoYes! So that's why I'm so disappointed I couldn't find a warning discouraging all use of pointer arithmetic.