Tag: gcc
-
Why does GCC copy object for each comparison in `std::ranges::max`?
10 Consider the following example (Godbolt): #include <vector> #include <iostream> #include <ranges> #include <algorithm> struct A { A() {} A( const A& ) { std::cout << "Copyn"; } A( A&& ) noexcept { std::cout << "Moven"; } A& operator=(const A&) { std::cout << "Copy assignedn"; return *this; } A& operator=( A&& ) noexcept { std::cout…
-
GCC removes a bounds check in the right operand of &&, but not in the left operand, why?
18 I have the following C/C++ code snippet: #define ARRAY_LENGTH 666 int g_sum = 0; extern int *g_ptrArray[ ARRAY_LENGTH ]; void test() { unsigned int idx = 0; // either enable or disable the check "idx < ARRAY_LENGTH" in the while loop while( g_ptrArray[ idx ] != nullptr /* && idx < ARRAY_LENGTH */ )…
-
How can a big number fit precisely into `double` with 32-bit GCC?
6 Consider the following code: #include <iostream> int main() { long long x = 123456789123456789; std::cout << std::fixed; auto y = static_cast<double>(x); // (1) std::cout << static_cast<long long>(y) << "n"; // (2) std::cout << y << "n"; std::cout << (x == static_cast<long long>(y)) << "n"; // (3) std::cout << static_cast<long long>(static_cast<double>(x)) << "n"; // (4)…
-
Why is a stray semicolon no longer detected by `-pedantic` modern compilers?
6 The following snippet generates compilation errors on adding -pedantic and -Werror on compilers that are a bit old. #include <cstdint> #include <iostream> int add(int a, int b){ return a + b; }; // <– stray semicolon int main (){ return 0; } However this does not happen newer compiler versions. Please find a matrix…
-
Is it ok to use std::ignore in order to discard a return value of a function to avoid any related compiler warnings?
10 I know that you can use static_cast<void>, but it just seems too verbose for me, and not reflecting the original intent that I want to discard a return value, not to cast it to anything. Recently I stumbled upon std::ignore, which can accept a value of any type, the name is clear and readable,…
-
How can I get a warning when comparing unsigned integers of different size in C and C++?
27 A common source of bugs in C or C++ is code like size_t n = // … for (unsigned int i = 0; i < n; i++) // … which can infinite-loop when the unsigned int overflows. For example, on Linux, unsigned int is 32-bit, while size_t is 64-bit, so if n = 5000000000,…
-
How to get a warning when comparing unsigned integers of different size in C and C++?
18 A common source of bugs in C or C++ is code like size_t n = // … for (unsigned int i = 0; i < n; i++) // … which can infinite-loop when the unsigned int overflows. For example on Linux unsigned int is 32-bit, while size_t is 64-bit, so if n = 5000000000,…
-
Why gcc is so much worse at std::vector vectorization of a conditional multiply than clang?
25 Consider following float loop, compiled using -O3 -mavx2 -mfma for (auto i = 0; i < a.size(); ++i) { a[i] = (b[i] > c[i]) ? (b[i] * c[i]) : 0; } Clang done perfect job at vectorizing it. It uses 256-bit ymm registers and understands the difference between vblendps/vandps for the best performance possible.…
-
No compiler warning for ‘const’ pointers of non POD types when overriding a method
7 An example where one is overriding POD return type without const in the derived class: struct B1 { virtual const int* f(); }; struct D1 : B1 { int* f() override; }; Compilers like Clang and GCC raise a warning: invalid covariant return type for ‘virtual int* D1::f()’ When same scenario is applied but…