[PATCH] D124038: [clang] Prevent folding of non-const compound expr
Eli Friedman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue May 10 12:54:37 PDT 2022
efriedma added a comment.
I think you're looking at old documentation? Here's what the current page (https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html) has to say:
In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object that only lives until the end of its full-expression. As a result, well-defined C code that takes the address of a subobject of a compound literal can be undefined in C++, so G++ rejects the conversion of a temporary array to a pointer. For instance, if the array compound literal example above appeared inside a function, any subsequent use of foo in C++ would have undefined behavior because the lifetime of the array ends after the declaration of foo.
As an optimization, G++ sometimes gives array compound literals longer lifetimes: when the array either appears outside a function or has a const-qualified type. If foo and its initializer had elements of type char *const rather than char *, or if foo were a global variable, the array would have static storage duration. But it is probably safest just to avoid the use of array compound literals in C++ code.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D124038/new/
https://reviews.llvm.org/D124038
More information about the cfe-commits
mailing list