gpt4 book ai didi

c++ - 来自用户定义文字的字符的整数序列,以字符串为参数

转载 作者:可可西里 更新时间:2023-11-01 18:39:57 26 4
gpt4 key购买 nike

目前,只有 double 可以在用户定义的文字中生成字符模板:

template <char...> double operator "" _x();
// Later
1.3_x; // OK
"1.3"_y; // C++14 does not allow a _y user-
// defined operator to parse that as a template of chars

有没有聪明的方法来生成 std::integer_sequence使用用户定义文字的字符。换句话说,_y(const char*, std::size_t) 的代码是什么?这样我就可以得到 std::integer_sequence<char, '1', '.', '3'>

最佳答案

在这个时间点,我们能(便携地)做的最好的事情就是一个宏技巧,如 for vtmpl::string 所示。 .基本上,我们创建一个访问列表,例如

"abcd" -> {(0 < sizeof "abcd"? "abcd"[0] : 0), (1 < sizeof "abcd"? "abcd"[1] : 0), ...}

…我们对其进行修剪以获得所需的结果。

第一步很容易通过 BOOST_PP_ENUM 完成,尽管递归宏也很好(来自 here 的定义):

#define VTMPL_SPLIT_1(s, x, m) m(s, x)
#define VTMPL_SPLIT_4(s, x, m) VTMPL_SPLIT_1 (s, x, m), VTMPL_SPLIT_1 (s, x+1 , m), VTMPL_SPLIT_1 (s, x+2 , m), VTMPL_SPLIT_1 (s, x+3 , m)
#define VTMPL_SPLIT_16(s, x, m) VTMPL_SPLIT_4 (s, x, m), VTMPL_SPLIT_4 (s, x+4 , m), VTMPL_SPLIT_4 (s, x+8 , m), VTMPL_SPLIT_4 (s, x+12 , m)
#define VTMPL_SPLIT_64(s, x, m) VTMPL_SPLIT_16 (s, x, m), VTMPL_SPLIT_16 (s, x+16 , m), VTMPL_SPLIT_16 (s, x+32 , m), VTMPL_SPLIT_16 (s, x+48 , m)
#define VTMPL_SPLIT_256(s, x, m) VTMPL_SPLIT_64 (s, x, m), VTMPL_SPLIT_64 (s, x+64 , m), VTMPL_SPLIT_64 (s, x+128, m), VTMPL_SPLIT_64 (s, x+194, m)
#define VTMPL_SPLIT_1024(s, x, m) VTMPL_SPLIT_256(s, x, m), VTMPL_SPLIT_256(s, x+256, m), VTMPL_SPLIT_256(s, x+512, m), VTMPL_SPLIT_256(s, x+768, m)

上面的用法看起来像这样(包括修剪):

#define VTMPL_STRING_IMPL(str, n) vtmpl::rtrim<vtmpl::value_list<decltype(*str), VTMPL_SPLIT_##n(str, 0, VTMPL_ARRAY_SPLIT)>>::type
#
#define VTMPL_STRING(str) VTMPL_STRING_IMPL(str, 64 )

rtrim 定义在 algorithms.hxx 中.

关于c++ - 来自用户定义文字的字符的整数序列,以字符串为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com