C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::came_from_new [[must_new]]

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 3 Sep 2026 22:19:22 +0100
On Thu, Sep 3, 2026 at 5:17 PM Jan Schultke wrote:
>
> This idea seems unimplementable to me because it's
> not observable whether you new'd an object.


If a constructor is marked as "[[must_new]]" then you can force a
compiler error for the following:

    SomeClass my_global_variable; // compiler error -- global
variable didn't use new

    int main(void)
    {
        SomeClass var; // compiler error -- stack variable didn't use new

        alignas(SomeClass) char unsigned buf[ sizeof(SomeClass) ];
        ::new(&buf) SomeClass; // compiler error -- placement new
doesn't count

        auto *p = new SomeClass; // yay!
    }

Received on 2026-09-03 21:19:41