Tag: clang
-
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…
-
What is _Nullable pointer in C?
6 What does _Nullable mean in following declaration? void foo(int *_Nullable ptr) gcc of version 11.4 doesn’t compile that code, because it treats _Nullable keyword as an argument name. Yet I see this keyword in man 7 pages: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html https://man7.org/linux/man-pages/man2/timer_gettime.2.html Godbolt c Share Improve this question Follow asked 11 hours ago Ержанов ЖалгасЕржанов Жалгас 6111…
-
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…
-
How to fix code to avoid warning -Wunsafe-buffer-usage
6 Before clang 16.0 I was able to compile all my projects using -Weveything without problems, fixing my code and paying attention to the (useful) warnings that this option gave. However, it seems that the -Wunsafe-buffer-usage was added in version 16 (my system updated to it recently) and then compiling any project is impossible without…
-
Is clang generating incorrect code for inline assembly?
9 I have some C code: #include "stdio.h" typedef struct num { unsigned long long x; } num; int main(int argc, char **argv) { struct num anum; anum.x = 0; __asm__("movq %%rax, %0n" : "=m" (anum.x) : "rax"(2)); printf("%llun",anum.x); } which I’m compiling and running on my (intel) Mac laptop. The output from the code…
-
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 is initializing a string to “” more efficient than the default constructor?
11 Generally, the default constructor should be the fastest way of making an empty container. That’s why I was surprised to see that it’s worse than initializing to an empty string literal: #include <string> std::string make_default() { return {}; } std::string make_empty() { return ""; } This compiles to: (clang 16, libc++) make_default(): mov rax,…
-
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…