I think you're absolutely right. But I think the problems go further than that.
How do we know that the Widget returned doesn't contain a pointer that will dangle?
Whether or not the returned pointer value will dangle depends on how long that pointer value ends up hanging around. For example:
Logger logger1;
Window window1;
auto widget1 = WidgetFactory{window1, logger1}.create_widget();
{
Window window2;
widget1 = WidgetFactory{window2, logger1}.create_widget();
/* The `Window*` member of (the newly assigned value of) widget1 is not yet dangling. */
auto pos2 = widget1.get_window_pos(); // presumably fine
}
/* The `Window*` member of widget1 is now dangling. */
auto pos1 = widget1.get_window_pos(); // likely UB
Not only is the proposed "no_dangling" annotation in the wrong place, it's insufficient. You need "real" lifetime annotations that can be applied to data members as well as parameters and return values. (As I've mentioned, [[clang::lifetime_bound]] is also insufficient, but can be interpreted as an element of an annotation system that is sufficient, so can arguably be considered a step in "the right direction".)
There are a number of abandoned and ongoing efforts to design and implement a system of "sufficient" lifetime annotations for C++. As far as I know, the only working implementation is the scpptool system (my project). My view is that the other designs are not (yet) viable primarily because they do not (yet) acknowledge the degree of run-time support required to be viable.
For example, the Rust system would not be viable without (the run-time-checked) std::RefCell<>. The scpptool system also provides vaguely corresponding run-time-checked elements. As far as I know, the other efforts do not integrate such corresponding run-time-checked elements and do not (yet) concede the necessity to do so.
So this is how the Widget and WidgetFactory might look with scpptool lifetime annotations:
class Widget {
LT(a) Window* window_; // LT(a) just associates a the lifetime label `a` with the member pointer
public:
Widget(Window& w LTP(a)) : window_(&w) { }
} LT(a) /* Just declaring the lifetime label(s) used by this type. */;
class WidgetFactory {
LT(a) Window* window_;
MSE_SUPPRESS_CHECK_IN_DECLSCOPE Logger* logger_ = nullptr; // we use a check-suppession directive here because nullptr values are not supported for raw pointers in the enforced safe subset
public:
WidgetFactory(Window& w LTP(a), Logger& l) : window_(&w), logger_(&l) { }
WidgetFactory(Window& w LTP(a)) : window_(&w) { }
Widget create_widget() LT_RETVAL(a) // just specifying the lifetime value(s) of the return value
{
if (logger_) logger_->log("Creating new Widget");
return Widget{*window_}; // returns object containing a pointer
}
} LT(a);
Which would result in the UB in the problematic example I gave being safely caught via static analysis (https://godbolt.org/z/zzxar8j7x). Note that the lifetime annotation of the create_widget() member function can be omitted because, like Rust, it is the same as the implicit default lifetime annotation. In fact, like Rust, most (non-constructor) function annotations can be omitted. Types that contain raw pointers will need lifetime annotations, but for the most part you're essentially just labeling which constructor parameters correspond to which (pointer) data members.
View all comments
Show parent comment
5
u/duneroadrunner Sep 17 '26
I think you're absolutely right. But I think the problems go further than that.
Whether or not the returned pointer value will dangle depends on how long that pointer value ends up hanging around. For example:
Not only is the proposed "no_dangling" annotation in the wrong place, it's insufficient. You need "real" lifetime annotations that can be applied to data members as well as parameters and return values. (As I've mentioned,
[[clang::lifetime_bound]]is also insufficient, but can be interpreted as an element of an annotation system that is sufficient, so can arguably be considered a step in "the right direction".)There are a number of abandoned and ongoing efforts to design and implement a system of "sufficient" lifetime annotations for C++. As far as I know, the only working implementation is the scpptool system (my project). My view is that the other designs are not (yet) viable primarily because they do not (yet) acknowledge the degree of run-time support required to be viable.
For example, the Rust system would not be viable without (the run-time-checked)
std::RefCell<>. The scpptool system also provides vaguely corresponding run-time-checked elements. As far as I know, the other efforts do not integrate such corresponding run-time-checked elements and do not (yet) concede the necessity to do so.So this is how the
WidgetandWidgetFactorymight look with scpptool lifetime annotations:Which would result in the UB in the problematic example I gave being safely caught via static analysis (https://godbolt.org/z/zzxar8j7x). Note that the lifetime annotation of the
create_widget()member function can be omitted because, like Rust, it is the same as the implicit default lifetime annotation. In fact, like Rust, most (non-constructor) function annotations can be omitted. Types that contain raw pointers will need lifetime annotations, but for the most part you're essentially just labeling which constructor parameters correspond to which (pointer) data members.