Constexpr:将字符串视图列表转换为字符数组列表

Constexpr: Convert a list of string views into a list of char arrays

我遇到了一个有趣的 constexpr 问题,但一直未能完全破解。我遗漏的最后一块拼图如下

// Given a constexpr array of string views
constexpr std::array<std::string_view, X> views = ...;

// Convert it to a list of char arrays
constexpr std::tuple<std::array<char, Xs>...> buffers = ...;

我的问题是为每个数组找到合适的大小。如何提取 views 中 string_views 的大小并将它们作为模板参数传递给另一个函数?

我可以为每个缓冲区使用相同的大小,大到足以容纳每个参数,但我想知道是否有优化它们大小的方法,因为信息在编译时是已知的。


我要解决的问题的完整描述。 (如果有人能想出更好的方法,也因为我觉得很有趣...)

我想创建一个将参数列表转换为名称-值对列表的宏。例如

// Adding types explicitly to show what I want to achieve.

int x;
float y;
double z;

using named_values_t = std::tuple<
    std::pair<const char*, int&>,
    std::pair<const char*, float&>,
    std::pair<const char*, double&>>;

named_values_t nv = MY_MACRO(x, y, z);

const char* 增加了很大的难度,但这是对第三方库的要求。

现在我知道这可以用 Boost.Preprocessor 来完成,但我想只使用 STL 和 constexpr 方法来避免为此添加提升。我也知道这在支持 constexpr std::string 的编译器上是微不足道的,但我使用的是 C++17.

使用 constexpr 函数可以轻松完成字符串处理,

// Split "one, two, three" into {"one", "two", "three"}.
template <size_t Count>
constexpr std::array<std::string_view, Count> split_arguments(std::string_view);

但是,我无法将这些 string_views 作为 char 指针直接传递,因为此时它们只是指向内存中更大数组(完整的 "one, two, three")的指针。要作为 const char* 传递,每个元素都需要以 null 结尾。

但是我们可以为每个 std::string_view 构建一个 std::array 并复制它的内容,这样我们就有了每个参数名称的 char 数组,这将为每个名字。

constexpr std::string_view args = "one, two, three";

constexpr std::array<std::string_view, 3> views = split_arguments<3>(args); // {"one", "two", "three"}

constexpr std::tuple<std::array<char, Xs>...> buffers = make_buffers<Xs...>(views);

这里我无法弄清楚如何将视图的长度作为模板参数传递给下一个函数。

这里的工作解决方案(使用更大的固定大小的缓冲区):https://gcc.godbolt.org/z/WKsbvb

固定大小的缓冲区解决方案是可以的,但是执行额外的步骤以使缓冲区适合其实际大小会很棒。

只要 views 是具有静态存储持续时间的 变量(而不是由 constexpr 函数调用创建的纯右值),您可以使用通常的方法实现auto& 模板参数和 std::index_sequence 技巧:

#include<array>
#include<string_view>
#include<tuple>
#include<utility>
#include<type_traits>
#include<cstddef>

namespace detail {
  template<std::size_t N>
  constexpr auto copy_string(std::string_view s) {
    std::array<char,N+1> ret{};  // zero-initialize
    for(std::size_t i=N;i--;) ret[i]=s[i];
    return ret;
  }
  template<auto &V,std::size_t ...II>
  constexpr auto buffers(std::index_sequence<II...>) {
    return std::make_tuple(copy_string<V[II].size()>(V[II])...);
  }
}

template<auto &V> constexpr auto buffers=
  detail::buffers<V>
  (std::make_index_sequence
   <std::tuple_size_v<std::remove_reference_t<decltype(V)>>>());

constexpr std::array<std::string_view, 3> views = {"C","++",""};

static_assert(std::is_same_v
              <decltype(buffers<views>),
               const std::tuple<std::array<char,2>,
                                std::array<char,3>,
                                std::array<char,1>>>);
static_assert(std::get<0>(buffers<views>)[0]=='C');
static_assert(std::get<1>(buffers<views>)[1]=='+');
static_assert(std::get<2>(buffers<views>)[0]=='[=10=]');