https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83769
Bug ID: 83769
Summary: Statement expression inside lambda defined and
evaluated in global scope fails to compile with
optimizations
Product: gcc
Version: 4.8.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: giel+gcc at mortis dot eu
Target Milestone: ---
Created attachment 43089
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43089&action=edit
small example reproducing the problem
Evaluating a statement expression (such as used by htons on glibc) inside a
lambda defined at global scope (used for Immediately Invoked Lambda Expression
(IILE) used as initializer for a global constant) successfully compiles at -O0
but fails to compile at -O1 or higher. I'm building with -std=c++11.
I'm getting this error message:
> <source>: In lambda function:
> <source>:9:29: error: statement-expressions are not allowed outside functions
> nor in template-argument lists
> addr.sin_port = htons(KLocalPort);
A reduced example is attached.
I initially discovered this on GCC 4.8.2 (after discovering that a release
build fails on CI, while a debug system on my local dev machine succeeds).
Testing on godbolt.org confirms that this behaviour was already present on GCC
4.5.3 already (-std=c++0x was necessary on that version) and is still present
on GCC 7.2.
I also tested Clang (on godbolt.org only) and notice that versions 3.0 up until
and including 3.5.1 have the same behaviour (succeed with -O0, fail with
complaint about statement expression at file scope with -O1). Clang 3.6 no
longer has this problem though.