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

            Bug ID: 125978
           Summary: contracts: violation comment() overly verbose with
                    reference returning subscript operators
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

```cpp
struct A {
  int x = 42;
  const int& operator[](int) const { return x; }
};

int main() {
  A foo;
  contract_assert(foo[0] == 67);
}
```

Compilation flags: "-std=c++26 -fcontracts"

Running this program, output:
```
contract violation in function int main() at foo.cpp:8: (((int)((const A*)(&
foo))->A::operator[](0)) == 67)
[assertion_kind: assert, semantic: enforce, mode: predicate_false, terminating:
yes]
terminate called without an active exception
Aborted (core dumped)
```

First line is overly noisy

Compare this to a value returning operator:
```cpp
struct A {
  int x = 42;
  int operator[](int) const { return x; }
};

int main() {
  A foo;
  contract_assert(foo[0] == 67);
}
```

Running it produces:
```
contract violation in function int main() at foo.cpp:8: foo[0] == 67
[assertion_kind: assert, semantic: enforce, mode: predicate_false, terminating:
yes]
terminate called without an active exception
Aborted (core dumped)
```

The comment() in first line is much more clear IMO
  • [Bug c++/125978] New: contr... ivan.lazaric.gcc at gmail dot com via Gcc-bugs

Reply via email to