[Bug c++/101277] New: Assert in constructor generates code for statically checkable expression

p.waydan at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Jul 1 06:26:04 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101277

            Bug ID: 101277
           Summary: Assert in constructor generates code for statically
                    checkable expression
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: p.waydan at gmail dot com
  Target Milestone: ---

Created attachment 51092
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51092&action=edit
-v output for Compiler Explorer compilation

Given the following code segment compiled with C++14 and above:

#include <cassert>

struct Positive { constexpr Positive(int x) {assert(x >= 0);} };

int foo(Positive) {return 1;}

int main()
{
    return foo(Positive{1});
}

GCC 10.1 and 10.2 will generate code to check if the assertion is true even
though it is statically checkable. This occurs for optimization levels -O0,
-Os, and -O2, but the assertion is elided for optimization levels -Og, -O1, and
-O3.

The assertion is always elided in GCC 9.4 and below as well as GCC 10.3 and
above.
Please see https://godbolt.org/z/3scPr1P7W for a comparison of x86-64 assembly
with different GCC versions.


More information about the Gcc-bugs mailing list