public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments
@ 2023-09-17  8:18 leni536 at gmail dot com
  2023-09-17  8:23 ` [Bug c++/111440] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: leni536 at gmail dot com @ 2023-09-17  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111440
           Summary: wrong-code for ternary with reference-compatible
                    arguments
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: leni536 at gmail dot com
  Target Milestone: ---

version: g++ 13.2.0
flags: -std=c++20 -O2 -pedantic-errors

Consider the following code:

```
template <typename T>
void foo();,

bool bar() {
    int * ptr = nullptr;
    const int * const cptr = nullptr;
    decltype(auto) res = true ? ptr : cptr;
    foo<decltype(res)>(); // calls void<int const*>();
    return &res == &ptr; // returns false
}
```

https://godbolt.org/z/cW5bxch5K

The observed behavior is described in the code comments.

The expected behavior is that the ternary expression is an lvalue of type
`const int * const` and the result refers to `ptr`.

https://timsong-cpp.github.io/cppwp/n4868/expr.cond#4.1 applies. The target
type is `const int * const&`, E1 is an lvalue of type `int *`.

`int *` is reference-compatible with `const int * const`, as `int**` is
convertible to `const int * const *`.

https://timsong-cpp.github.io/cppwp/n4868/dcl.init.ref#5.1.1 applies, a
reference of type `const int * const&` can be initialized by an lvalue of type
`int*`, and the reference binds directly.

The wording for "reference-compatible" was changed in the resolution of
https://cplusplus.github.io/CWG/issues/2352.html, so earlier standard modes are
probably similarly affected.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111440] wrong-code for ternary with reference-compatible arguments
  2023-09-17  8:18 [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments leni536 at gmail dot com
@ 2023-09-17  8:23 ` pinskia at gcc dot gnu.org
  2023-09-17  8:28 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17  8:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note clang changed between version 9 and 10 for the defect report it seems ...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111440] wrong-code for ternary with reference-compatible arguments
  2023-09-17  8:18 [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments leni536 at gmail dot com
  2023-09-17  8:23 ` [Bug c++/111440] " pinskia at gcc dot gnu.org
@ 2023-09-17  8:28 ` pinskia at gcc dot gnu.org
  2023-09-17  8:56 ` [Bug c++/111440] wrong-code for ternary with reference-compatible arguments due to C++ defect report 2352 leni536 at gmail dot com
  2023-09-17  9:01 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17  8:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang change:
https://github.com/llvm/llvm-project/commit/f041e9ad706aee7987c5299427c33424fcabbd0d

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111440] wrong-code for ternary with reference-compatible arguments due to C++ defect report 2352
  2023-09-17  8:18 [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments leni536 at gmail dot com
  2023-09-17  8:23 ` [Bug c++/111440] " pinskia at gcc dot gnu.org
  2023-09-17  8:28 ` pinskia at gcc dot gnu.org
@ 2023-09-17  8:56 ` leni536 at gmail dot com
  2023-09-17  9:01 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: leni536 at gmail dot com @ 2023-09-17  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Lénárd Szolnoki <leni536 at gmail dot com> ---
Note that GCC seems to implement CWG2352 outside of ternary operators.

https://godbolt.org/z/rnMcPqE7b

```
template <typename T>
void foo();

bool bar() {
    int * ptr = nullptr;
    const int * const &ref = ptr;
    return &ref == &ptr; // returns true
}
```

from GCC 10 `bar` returns true.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111440] wrong-code for ternary with reference-compatible arguments due to C++ defect report 2352
  2023-09-17  8:18 [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments leni536 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-09-17  8:56 ` [Bug c++/111440] wrong-code for ternary with reference-compatible arguments due to C++ defect report 2352 leni536 at gmail dot com
@ 2023-09-17  9:01 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Lénárd Szolnoki from comment #3)
> Note that GCC seems to implement CWG2352 outside of ternary operators.

Yes that was done in GCC 10 (see PR 91844).

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-17  9:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-17  8:18 [Bug c++/111440] New: wrong-code for ternary with reference-compatible arguments leni536 at gmail dot com
2023-09-17  8:23 ` [Bug c++/111440] " pinskia at gcc dot gnu.org
2023-09-17  8:28 ` pinskia at gcc dot gnu.org
2023-09-17  8:56 ` [Bug c++/111440] wrong-code for ternary with reference-compatible arguments due to C++ defect report 2352 leni536 at gmail dot com
2023-09-17  9:01 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).