C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 30 Apr 2024 16:23:49 +0200
Use `consteval` constructor, then only static values can be used to init object:

```
struct StaticString
{
    consteval StaticString(const char* c)
    {

    }

    StaticString()
    {

    }

    StaticString(const StaticString& ss)
    {

    }
};

int main()
{
    StaticString s = {"aaaa"}; //ok

    const char* cc = "";
    StaticString s2 = {cc}; //error
}
```

wt., 30 kwi 2024 o 16:05 Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> 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).
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-04-30 14:24:03