how do you like the name "lifetimebound" instead?

Jul 22, 2025 8:16:03 PM Frederick Virchanza Gotham via Std-Proposals <std-proposals@lists.isocpp.org>:

On Tue, Jul 22, 2025 at 1:38 PM Jan Schultke wrote:

Another way to use the c_str() member function you've mentioned,
without involving a variable would be:

void f(const char*);
wxString mystr( wxS("Hello I'm a monkey") );
f(mystr.c_str());


We want an attribute to express the following:

"The object returned from this function can generate handles that
become invalid when the object is destroyed (i.e. the generated handle
does not out-live the object). Therefore, if you use the returned
object to generate a handle, then you must ensure that the object is
still in existence when the handle is used. To ensure this, either (a)
store the object in a local variable, or (b) use any generated handles
before the full expression is evaluated.".

I think we would need a pair of attributes to make this work. For
instance 'wxString' would need a method marked as 'must_store':

    struct wxString {
        [[must_store]] wxCStr c_str(void) const;
    };

And the the class 'wxCStr' would need to mark a method to indicate
that it generates a handle that does not out-live the object:

    struct wxCStr {
        [[generates_temporary_handle]] operator wchar_t const *(void) const;
    };

Actually on second thought, if "wxCStr::operator wchar_t*" is marked
as [[generates_temporary_handle]], then maybe "wxString::c_str"
doesn't need to be marked at all. Actually I think the marking in the
'wxCStr' class is enough.

So forget about [[must_store]]. Instead I'd like to propose
[[generates_temporary_handle]].
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals