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

            Bug ID: 125587
           Summary: [contracts] "contract condition is not constant" when
                    post being used
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: oguzhankatli at gmail dot com
  Target Milestone: ---

Compiler error: "contract condition is not constant" is happening when a post
contract is used in constexpr function and assign return value to const or
constexpr variable. I believe using post in constexpr function leads to runtime
call silently, and since the return value force function to call at compile
time creates this error. 

to reproduce the error:

https://godbolt.org/z/rvGxhTo39

#include <contracts>

constexpr int f(int i)
    pre(i > 0)
    post(res: res > 0)
{
    return i;
}

int main(int argc, char *[]) {
    auto s = f(2);
    const auto e = f(2);  // this line forces to call f() in compile time hence
the error happens.
    return s;
}

Reply via email to