我遇到了一个有趣的 constexpr 问题,但一直未能完全破解。我缺少的最后一 block 拼图如下
// 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 数组,即将为每个名称生成一个以 null 结尾的内存段。
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);
在这里我无法弄清楚如何将 View 的长度作为模板参数传递给下一个函数。
这里的工作解决方案(使用更大的固定大小的缓冲区):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]=='\0');
关于c++ - Constexpr:将字符串 View 列表转换为字符数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64469841/