Date: Tue, 07 Apr 2026 15:28:43 +0300
Colleagues,
I would like to submit a separate proposal, dependent on the original proposal "Check of scoped-bounded lifetime objects at compile time". Perhaps I should have done this in a single original proposal, but I believe it's appropriate to split them into two separate, albeit related, one
The idea is to automatically treat any variable with a type marked as [[scope_lifetime]] and returned by a method on an object of type [[scope_owner]] as a *borrowed value* bound to a specific owner object. That is, the compiler can explicitly track a relationship of the form: owner -> returned type -> variable.
This is only rough warning-based compile time analysis, not a strict safety borrow checking system.
Since in C++ *const* does not guarantee the absence of actual mutation, and data invalidation of an object often depends not only on the method signature but also on operations, container type, and even runtime conditions, this proposal is only about conservative compiler diagnostics with warnings only.
The basic rule is proposed as follows: if after obtaining a [[scope_lifetime]] value, a non-const method of the owner is called, or the owner is passed to a function by non-const reference or pointer, or the owner is moved, assigned, or destroyed, then all previously borrowed values obtained from it are considered *potentially invalidated*, and the compiler may issue a warning upon their subsequent use.
auto it = v.begin();
v.push_back(x);
use(it); // possible invalidation bug
It is deliberately proposed *not to cover raw pointers and references*, but to limit it only to class-based handle types: iterators, class-based view/object handle types, proxy access types, and similar objects.
Separately, the case where the owner is not directly passed into a function is interesting, for example, as in std::sort:
auto h = v.begin();
std::sort(v.begin(), v.end()); // non-const borrowed arguments obtained from v
use(h); // warning
Here, it can be considered that if a function receives *non-const arguments of type [[scope_lifetime]]* previously obtained from a [[scope_owner]] object, then such a call may also imply mutation of the corresponding owner, and subsequent use of previously obtained [[scope_lifetime]] values from the same owner should trigger a warning.
I would especially appreciate feedback on this and the previous proposal, as they offer C++ tools for compile-time diagnosis of the most common errors related to reference type invalidation and automatic lifetime checking of certain data types, which are sorely lacking for safety C++ programming.
Kind regards,
Ryabikov Aleksandr
I would like to submit a separate proposal, dependent on the original proposal "Check of scoped-bounded lifetime objects at compile time". Perhaps I should have done this in a single original proposal, but I believe it's appropriate to split them into two separate, albeit related, one
The idea is to automatically treat any variable with a type marked as [[scope_lifetime]] and returned by a method on an object of type [[scope_owner]] as a *borrowed value* bound to a specific owner object. That is, the compiler can explicitly track a relationship of the form: owner -> returned type -> variable.
This is only rough warning-based compile time analysis, not a strict safety borrow checking system.
Since in C++ *const* does not guarantee the absence of actual mutation, and data invalidation of an object often depends not only on the method signature but also on operations, container type, and even runtime conditions, this proposal is only about conservative compiler diagnostics with warnings only.
The basic rule is proposed as follows: if after obtaining a [[scope_lifetime]] value, a non-const method of the owner is called, or the owner is passed to a function by non-const reference or pointer, or the owner is moved, assigned, or destroyed, then all previously borrowed values obtained from it are considered *potentially invalidated*, and the compiler may issue a warning upon their subsequent use.
auto it = v.begin();
v.push_back(x);
use(it); // possible invalidation bug
It is deliberately proposed *not to cover raw pointers and references*, but to limit it only to class-based handle types: iterators, class-based view/object handle types, proxy access types, and similar objects.
Separately, the case where the owner is not directly passed into a function is interesting, for example, as in std::sort:
auto h = v.begin();
std::sort(v.begin(), v.end()); // non-const borrowed arguments obtained from v
use(h); // warning
Here, it can be considered that if a function receives *non-const arguments of type [[scope_lifetime]]* previously obtained from a [[scope_owner]] object, then such a call may also imply mutation of the corresponding owner, and subsequent use of previously obtained [[scope_lifetime]] values from the same owner should trigger a warning.
I would especially appreciate feedback on this and the previous proposal, as they offer C++ tools for compile-time diagnosis of the most common errors related to reference type invalidation and automatic lifetime checking of certain data types, which are sorely lacking for safety C++ programming.
Kind regards,
Ryabikov Aleksandr
Received on 2026-04-07 12:28:49
