C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 04 Sep 2026 10:37:02 -0300
On Friday, 4 September 2026 07:00:13 Brasilia Standard Time Simon Schröder via
Std-Proposals wrote:
> BTW, in Qt I don't
> always use 'new'. 'new' is only necessary when the QObject also gets a
> parent (and the parent is destroyed before this object). E.g., a QDialog I
> usually create as a stack variable. The main window is another one I create
> on the stack. In this case I specifically don't want constructors to be
> marked as [[must_new]]. I guess the same would hold for wxWidgets. And if
> these are the only use cases, I'd rather have libraries follow in the
> footsteps of Wt instead of standardizing new C++ features to support an
> outdated design pattern from the 90's

Except if the new design pattern is imposing an unnecessary cost. As you've
said yourself, you *can* create such objects on the stack and allocating space
on the stack usually has a sub-nanosecond cost, while allocating on the heap
is at a minimum 10x more expensive. This also includes whether such objects
can be members or not: a widget with a dozen or two sub-widgets could avoid a
dozen or two heap allocations and thus double-indirection when accessing the
private data of such sub-widgets.

It's all well and good to help programmers avoid mistakes and use RAII as much
as possible. It does not follow std::unique_ptr is the solution.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-09-04 13:37:10