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 01:22:59 +0100
On Sat, May 4, 2024 at 9:52 PM Thiago Macieira wrote:
>
> I can do it with:
>
> template<size_t... N> consteval auto(const char (&... strings)[N])


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

And here it is copy-pasted:

#include <cstddef> // size_t
#include <algorithm> // copy_n
#include <array> // array
#include <iostream> // cout, endl

template<std::size_t... N>
consteval auto Concat(char const (&... strings)[N])
{
    using std::size_t;
    constexpr size_t sum = ((N-1u) + ...);
    std::array<char, sum+1u> retval{};
    size_t index = 0u;
    ((std::copy_n(strings, N-1u, retval.begin() + index), index += N-1u), ...);
    retval[sum] = '\0';
    return retval;
}

int main(void)
{
    auto const a = Concat("abc", "def", "ghi");

    std::cout << &a.front() << std::endl;
}

Received on 2024-05-05 00:23:08