C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Expand arrays with known sizes using ellipses

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Thu, 21 Sep 2023 22:48:35 +0300
On 9/21/23 22:12, Chris Gary via Std-Proposals wrote:
> This needs to be a thing:
>
> `template< char ...chars_ >
> struct char_list
> {
> // etc...
> };
>
> template< std::size_t size_ >
> constexpr char only_constexpr_example( const char (&str)[size_] ) noexcept
> {
> // In a constexpr context, the size and contents of str are "known".
>
> // Really no different than a numeric UD-literal.
> //
> // This declaration makes the function constexpr-only
> // since it requires knowledge of str's contents at compile time.
> using chars = char_list< str... >;
>
> // If the size of an array is known, this can be the
> // same as an element-by-element copy construction
> // at runtime.
> constexpr char str2[]{ str... };
>
> return str2[0];
> }`

This would break ODR. Even if constexpr, `only_constexpr_example("abc")`
and `only_constexpr_example("def")` refer to the same function, yet its
body is different as it uses different specializations of `char_list`.

Received on 2023-09-21 19:48:38