C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Function only accepts parameter that will persist

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 8 May 2024 09:33:36 +0100
On Tue, May 7, 2024 at 12:10 PM Sebastian Wittmeier wrote:
>
> If you want to introduce new type modifiers, do not expect
> C++26, perhaps not even C++29.
>
> The type system is very integral to the core language, and
> also many templates expect to fully exhaust the type space
> with definitions.


Instead of marking the variable, how about marking the function? So
the two constructors and two assignment operators would be:

efficient_string(char const *)
{
    // copy the string
}
efficient_string(char const *) static_duration_const_arguments
{
    // don't copy the string
}
efficient_string &operator=(char const *)
{
    // copy the string
}
efficient_string &operator=(char const *) static_duration_const_arguments
{
    // don't copy the string
}

And so then when the compiler encounters:

    efficent_string s;
    s = "monkey";

It calls the 'static_duration_const_arguments' function because it
knows "monkey" is static-duration and won't ever change.


> Fundamentally changing the language with only plain old microcontrollers
> with ROM in mind as a technological use-case seems a bit short-sighted
> for a language change, one would do every 40 years or so (if at all).


Actually this change isn't even needed for microcontrollers. For
example I'm currently programming the Arduino SAM D21 microcontroller
in a product, and the non-volatile memory is from address to 0x0 to
0x20000000, and volatile SRAM is from address 0x20000000 to
0x20008000, so I can just do:

efficient_string(char const *const p)
{
    if ( (uintptr_t)p < 0x20000000 )
    {
        // don't copy the string
    }
    else
    {
        // copy the string
    }
}

But I can't do this on an x86_64 desktop PC running Linux or
MS-Windows, hence why I'm talking about being able to identify at
compile time if a compound literal (or string literal) is
static-duration and will never change.

Received on 2024-05-08 08:33:48