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: Sun, 5 May 2024 13:00:14 +0100
On Sun, May 5, 2024 at 4:00 AM Thiago Macieira wrote:
>
> On Saturday 4 May 2024 17:22:59 GMT-7 Frederick Virchanza Gotham wrote:
>>
> > Just before we go any further, I want to be sure that I understand
> > what you mean here. Do you mean the following:
> >
> > https://godbolt.org/z/aaarja5Ps
>
> Yes.


So in the future we want the following to be valid code?

consteval auto Concat(std::initializer_list<std::string_view> const args)
{
    using std::size_t;
    size_t sum = 0u;
    for ( auto const sv : args ) sum += sv.size();
    std::array< char, sum + 1u > retval{};
    size_t index = 0u;
    for ( auto const sv : args )
    {
        std::copy(sv.cbegin(), sv.cend(), retval.begin() + index);
        index += sv.size();
    }
    retval[sum] = '\0';
    return retval;
}

Received on 2024-05-05 12:00:26