Bug 115233 - constexpr RVO result is not determined as constant epression
Summary: constexpr RVO result is not determined as constant epression
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2024-05-26 12:16 UTC by Tilir
Modified: 2024-05-27 15:52 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-05-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tilir 2024-05-26 12:16:17 UTC
Consider I have this type:

---

struct Type {
  int a;
  const int& b;
  constexpr Type() : a(1), b(a) {}
};

constexpr Type t1;
constexpr int c11 = t1.a;
constexpr int c12 = t1.b;

---

c11 and c12 are fine for both gcc and clang

Now lets introduce constexpr function which is subject to RVO:

constexpr auto get() {
  return Type();
}

Now try this:

constexpr Type t2 = get();
constexpr int c2 = t2.a;

This code compiles fine with clang but not with gcc.

Live example on godbolt: https://godbolt.org/z/zf3YvebbG

According to [expr.const] and [class.copy.elision] result of constexpr function is necessary RVO and is contant expression, so t2 construction is fine and t2.a is constant expression as well.
Comment 1 Drea Pinski 2024-05-26 12:18:48 UTC
It also compiles fine with MSVC.
Comment 2 Drea Pinski 2024-05-26 12:20:04 UTC
Confirmed.