#clang
16 posts tagged #clang.
Keeping the early exit and getting vector code on a 64-byte compare
Cut a 64-byte early-exit compare into chunks that exit between chunks, and GCC and Clang compile each chunk to a vector test: 12 instructions with 32-byte chunks and AVX2 flags on GCC, 13 on Clang. Instruction counts for six forms on four compilers, one way to read them, and std::simd on GCC.
Five ways to compare 64 bytes, and what GCC, Clang and MSVC make of them
In every build I made, array equality, memcmp and std::equal compile to the same instructions as each other for a 64-byte block. Written as a loop with an early exit, the compare stays scalar on Clang and MSVC, and GCC vectorizes it only with AVX2 flags and behind alignment checks. Ten builds on x86-64, the compiler messages that explain them, three public bug reports, and a control that vectorizes.
An empty constructor written {} stops {} from zeroing the object
With the empty constructor spelled = default in the class, T() and T{} zero a class and the class stays trivial. Spelled {} or defined outside the class, neither zeroes it and the class is not trivial. On GCC 16.2 and Clang 22.1 the difference shows in constant expressions, in traits and in what a read of an unset member gives.
A contract assertion labelled enforced still terminates under ignore
P4451R2, adopted at Búzios, adds a label that makes one contract assertion terminate whatever the build asks. On GCC and Clang branch builds a labelled assertion kept running under ignore, and a violated one ended the program in 12 of 12 runs. GCC 16.2 rejects the syntax.
Clang 23.1 -Wlifetime-safety warns on 6 of 7 dangling cases, GCC 16.2 on 1
On seven small dangling cases, Clang 23.1 with -Wlifetime-safety warns on six. GCC 16.2 with -Wall -Wextra -Wdangling-pointer=2 -Wdangling-reference gives a correct dangling report on one, from inside a standard header. Without the flag Clang already warned on three. The flag adds an escaping lambda capture, a reference across push_back and a view field set from a parameter, and it flags one correct program. Trunk renamed the push_back check.
What the initialization profile says about code it was not written for
I ran the examples of P4222R3, the initialization profile, on the two experimental Compiler Explorer builds, plus one case from outside the paper. Both builds reject the paper's wrong examples and accept its right ones, with a handful of differences between them. Neither build says anything about a thread that starts before the member it counts into is initialized.
A thread that starts before its counter exists
A std::jthread member declared before the data it uses breaks at both ends of the object's life. ThreadSanitizer reports the construction end, exit 66, on GCC 16.2 and clang trunk. The destruction end is silent for an atomic and reported for a vector. GCC 16.2 -Wall -Wextra -Weffc++ and clang trunk -Weverything say nothing about the order.
A type that is trivially copyable and cannot be copied
std::is_trivially_copyable answers a narrower question than its name suggests. There is a type for which it is true while the type is neither copy-constructible nor copy-assignable, MSVC says so, GCC and Clang disagree, and the paper that documents it says MSVC is the one following the standard.
Profiles now run on Compiler Explorer: what P4415R0 reports and what I ran on both builds
P4415R0 reports two working implementations of C++ profiles, one in a Clang fork and one in a GCC fork, both on Compiler Explorer. I ran examples from the paper on both builds on 25 October. The GCC build rejects an unmarked owning factory, a leaked owner, a vector pointer used after push_back and a returned local address under the invalidation profile. The Clang build has no such profile and compiles the same file without complaint.
Constexpr constructors with virtual bases work in GCC 16 and Clang trunk
P3533R2 lifts the rule that a constexpr constructor cannot belong to a class with a virtual base. GCC 16.1 accepts it with -std=c++26 and Clang trunk accepts it, while GCC 15.3 and Clang 23.1 reject it. One case still differs: GCC 16 refuses a virtual call through a reference to the virtual base in a constant expression, and Clang trunk accepts it.
C++26 reflection runs on a compiler you already have
The received wisdom is that trying P2996 means the Bloomberg clang fork. It does not. GCC 16.1 has shipped reflection since April behind -freflection, using the standard <meta> header, and it runs the same code. The two implementations do disagree about what display_string_of prints.
C++26 tightened unevaluated strings, and GCC did half of it
P2361R6 makes encoding prefixes and numeric escapes ill-formed in the strings that only exist at compile time. GCC 16.1 rejects the prefixes and accepts the escapes; Clang 22.1 rejects both. The same source compiles on one and fails on the other, which is worth knowing before you rely on either.
C++29 mode is open, and the compilers already disagree
LLVM 23.1 shipped with -std=c++2d, a mode for the standard after C++26, and GCC trunk has one too. Both report __cplusplus as 202700. They do not agree about C++26 itself: GCC says 202603, which is the ratified value, and clang still says 202400.
LLVM 23.1 shipped, and one of its changes is silent
Clang 23.1 landed on 25 August with partial expansion statements, modules dependency discovery, and a batch of core-issue fixes. It also elides more dead stores than 22 did, which is the one change that alters behaviour without saying anything at build time.
A C++26 structured binding that GCC and Clang disagree about
C++26 lets a structured binding declaration be the condition of an if. GCC 16.1 and Clang 22.1 both implement it and both agree on ordinary structs. Put a tuple-protocol type in the condition and evaluate it at compile time, and GCC rejects the program while Clang accepts it. The reproducer is twenty lines and needs no library.
Clang's lifetimebound warns about a copy, and that is the problem
The lifetimebound attribute lets -Wdangling see through a function call, which is how clang catches a string_view into a temporary. It has no conditional form, so a function that returns a copy on some instantiations and a reference on others has to pick between missing real dangles and warning about correct code. A proposal on the LLVM forum wants to close that gap.