llvm/llvm-project

`[[gnu::weak]]` disables constant evaluation of constexpr variables in C++14

Open

#141.797 geöffnet am 28. Mai 2025

Auf GitHub ansehen
 (12 Kommentare) (1 Reaktion) (0 zugewiesene Personen)C++ (10.782 Forks)batch import
c++11c++14clang:frontenddiverges-from:gccgood first issue

Repository-Metriken

Stars
 (26.378 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 1T 2h) (1.000 gemergte PRs in 30 T)

Beschreibung

GCC and Clang disagree on their handling of [[gnu::weak]] before C++17 https://gcc.godbolt.org/z/s7q67vqjz

struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;

While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC

In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode

Contributor Guide