C++ Logo

std-proposals

Advanced search

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

From: Chris Gary <cgary512_at_[hidden]>
Date: Thu, 21 Sep 2023 13:12:09 -0600
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];
}`

Received on 2023-09-21 19:12:21