如何在编译时从constexpr字符数组组成字符串字面值?



我试图创建一个constexpr函数,将const字符数组连接到一个数组。我的目标是通过引用一个总是连接两个const char*的专用变量模板join来递归地做到这一点。但是编译器不喜欢它,并且抛出一个我无法处理的错误消息。

我已经检查了这个主题,但不幸的是没有一个直接的答案。

代码:

#include <type_traits>
#include <cstdio>
#include <iostream>
constexpr auto size(const char*s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
}
return i;
}
template <const char* S1, typename, const char* S2, typename>
struct join_impl;
template <const char* S1, int... I1, const char* S2, int... I2>
struct join_impl<S1, std::index_sequence<I1...>, S2, std::index_sequence<I2...>>
{
static constexpr const char value[]{ S1[I1]..., S2[I2]..., 0 };
};
template <const char* S1, const char* S2>
constexpr auto join
{
join_impl<S1, std::make_index_sequence<size(S1)>, S2, std::make_index_sequence<size(S2)>>::value
};
template <const char* S1, const char* S2, const char*... S>
struct join_multiple
{
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>::value;
};
template <const char* S1, const char* S2>
struct join_multiple<S1, S2>
{
static constexpr const char* value = join<S1, S2>;
};

constexpr const char a[] = "hello";
constexpr const char b[] = "world";
constexpr const char c[] = "how is it going?";
int main()
{
// constexpr size_t size = 100;
// char buf[size];
// lw_ostream{buf, size};
std::cout << join_multiple<a, b, c>::value << std::endl;
}

误差:

<source>:33:82: error: qualified name refers into a specialization of variable template 'join'
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>::value;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
<source>:25:16: note: variable template 'join' declared here
constexpr auto join
^
<source>:33:34: error: default initialization of an object of const type 'const char *const'
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>::value;
^
= nullptr
2 errors generated.
ASM generation compiler returned: 1
<source>:33:82: error: qualified name refers into a specialization of variable template 'join'
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>::value;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
<source>:25:16: note: variable template 'join' declared here
constexpr auto join
^
<source>:33:34: error: default initialization of an object of const type 'const char *const'
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>::value;
^
= nullptr
2 errors generated.
Execution build compiler returned:

我错过了什么?

作为备选方案,为了避免构建临时字符数组,您可以使用类型(字符序列)并仅在末尾创建字符数组变量,如:

constexpr auto size(const char*s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
}
return i;
}
template <const char* S, typename Seq = std::make_index_sequence<size(S)>>
struct as_sequence;
template <const char* S, std::size_t... Is>
struct as_sequence<S, std::index_sequence<Is...>>
{
using type = std::integer_sequence<char, S[Is]...>;
};
template <typename Seq>
struct as_string;
template <char... Cs1>
struct as_string<std::integer_sequence<char, Cs1...>>
{
static constexpr const char c_str[] = {Cs1..., ''};
};
template <typename Seq1, typename Seq2, typename... Seqs>
struct join_seqs
{
using type = typename join_seqs<typename join_seqs<Seq1, Seq2>::type, Seqs...>::type;
};
template <char... Cs1, char... Cs2>
struct join_seqs<std::integer_sequence<char, Cs1...>, std::integer_sequence<char, Cs2...>>
{
using type = std::integer_sequence<char, Cs1..., Cs2...>;
};
template <const char*... Ptrs>
const auto join =
as_string<typename join_seqs<typename as_sequence<Ptrs>::type...>::type>::c_str;

演示

这里有两个问题

首先,join是模板变量,所以它不包含所谓的value_type,它本身就是一个值,所以你的join_multiple应该是

template <const char* S1, const char* S2, const char*... S>
struct join_multiple {
static constexpr const char* value = join<S1, join_multiple<S2, S...>::value>;
};

第二,也是不太重要的,index_sequence的整数类型是size_t而不是int,所以join_impl的部分专门化应该是(这不是必要的,但使用size_t以外的类型会导致GCC错误地拒绝它)

template <const char* S1, size_t... I1, const char* S2, size_t... I2>
struct join_impl<S1, std::index_sequence<I1...>, S2, std::index_sequence<I2...>> {
static constexpr const char value[]{ S1[I1]..., S2[I2]..., 0 };
};

演示

相关内容

  • 没有找到相关文章

最新更新