Issue 60936
Summary Aggregate construction of an lvalue class object not considered constant _expression_
Labels new issue
Assignees
Reporter danakj
    Construction as an rvalue works, but not as an lvalue.

https://godbolt.org/z/r3d17jMjM

```cpp
struct S {
    // Clang cant constexpr construct this as an lvalue without the constructor.
    // constexpr S(int i) : i(i) {}

    int i;
 int *p = &i;
    constexpr const int* ptr() const { return &i; }
};

// Not constant _expression_ in GCC.
static_assert(S{2}.ptr() != nullptr);
static_assert(S{2}.p != nullptr);

// Is a constant _expression_.
static_assert(*S{2}.ptr() == 2);
static_assert(*S{2}.p == 2);

static_assert([]() constexpr {
    // Not a constant _expression_ in Clang without a user-defined constructor.
    auto r = S{2};

 // Is a constant _expression_.
    return r.ptr() != nullptr && r.p != nullptr;
}());
```

Generates a compiler error that the lambda is not a constant _expression_.

Uncommenting the constructor in S makes the example compile. GCC and MSVC accept this without the constructor.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to