C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Check of scoped-bounded lifetime objects at compile time

From: Andre Kostur <andre_at_[hidden]>
Date: Mon, 6 Apr 2026 14:35:18 -0700
On Mon, Apr 6, 2026 at 1:41 PM Рябиков Александр <rsashka_at_[hidden]> wrote:
>
> I am not claiming that this is a pervasive problem across all C++ code. However, it does arise for types whose meaning must be strictly confined to the current scope.

Still curious for specific examples of such types that must have that
constraint that cannot be violated for any reason.

> Such use cases are primarily needed to implement safe memory management and deterministic concurrent programming.

Still looking for concrete examples. And that sounds more like a
restriction on the instance of a type, not something inherent to a
type. But concrete examples may clarify this point.

> The problem I am trying to address is that the language should be able to guarantee that "this type may exist only in local scope and is guaranteed to be destroyed when the current scope exits"

Not sure why you're adding the destruction clause in there. All
local scope objects are guaranteed to be destroyed at the end of the
scope.

> Without language-level (compiler-enforced) guarantees, it is not possible to implement the use cases described above reliably; at present, they can only be approximated through conventions, code review, or custom tooling.

Still unsure whether this is something that we need to enshrine in the
Standard and force every compiler to enforce.


Does this mean that the function:

    void addThingsToContainer(std::vector<T> & vec) {
        vec.push_back(T{});
    }

Should fail to compile because I'm adding it to a vector passed by
reference? Even if the next function is:

    void addThingsTwice() {
        std::vector<T> v;
        addThingsToContainer(v);
        addThingsToContainer(v);
    }

Note that the first one has external linkage, and thus may be called
from a different translation unit.

Received on 2026-04-06 21:35:30