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: Tue, 30 Apr 2024 15:05:36 +0100
On Tue, Apr 30, 2024 at 3:01 PM I wrote:
>
> But maybe we need some sort of class that has the option of copying
> the object, something like this:
>
> https://godbolt.org/z/rz6dfvGPo


Just one other thing I want to mention . . . In the following constructor:

    String(char const *const arg) noexcept(false)
    {
        std::cout << "copying\n";
        p = new char[ std::strlen(arg) + 1u ];
        should_delete = true;
        std::strcpy( const_cast<char*>(p), arg );
    }

You could check if 'arg' points to a page of memory that is read-only.
If it's read-only then use the non-copying constructor instead. On
MS-Windows, 'VirtualProtect' gives us this info. On Linux, 'mprotect'
give us this info. On a microcontroller, check if the address is in
the range 0x0 - 0x2000 (i.e. read-only ROM), or from 0x2000 onwards
(i.e. volatile RAM).

Received on 2024-04-30 14:05:51