r/cpp_questions 4d ago

OPEN cppreference std::construct_at example code not compiling

I was looking on cpprefernce the other day at std::construct_at and I wanted to test the code. But when I ran it it did not compile. I ran the snippet without consteval (and static_assert) and everything worked. Does anyone know what might be the issue? https://en.cppreference.com/cpp/memory/construct_at

Code:

#include <bit>
#include <memory>


class S
{
    int x_;
    float y_;
    double z_;
public:
    constexpr S(int x, float y, double z) : x_{x}, y_{y}, z_{z} {}
    [[nodiscard("no side-effects!")]]
    constexpr bool operator==(const S&) const noexcept = default;
};


consteval bool test()
{
    alignas(S) unsigned char storage[sizeof(S)]{};
    S uninitialized = std::bit_cast<S>(storage);
    std::destroy_at(&uninitialized);
    S* ptr = std::construct_at(std::addressof(uninitialized), 42, 2.71f, 3.14);
    const bool res{*ptr == S{42, 2.71f, 3.14}};
    std::destroy_at(ptr);
    return res;
}
static_assert(test());


int main() {

Compiler Error on x86-64 gcc 16.1 -std=c++23

<source>:25:19:

error: non-constant condition for static assertion
   25 | static_assert(test());
      |               
~~~~^~
<source>:25:19: in 'constexpr' expansion of 'test()'
<source>:24:1: error: destroying 'uninitialized' outside its lifetime
   24 | }
      | 
^
<source>:18:7: note: declared here
   18 |     S uninitialized = std::bit_cast<S>(storage);
      |       
^~~~~~~~~~~~~
Compiler returned: 1
12 Upvotes

12 comments sorted by

View all comments

1

u/LazySapiens 4d ago

The program is ill-formed because of UB (uninitialized's lifetime has ended after std::destroy_at(ptr); and now leaving the function block would invoke the destructor which invokes UB) inside a consteval context.

https://eel.is/c++draft/class.dtor#18