c++ - 将有状态 lambda 应用于整数序列值
问题描述
我正在尝试实现数字文字运算符模板。
#include <string_view>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <boost/mp11/integer_sequence.hpp>
#include <boost/mp11/algorithm.hpp>
using namespace boost::mp11;
template <char... Cs>
[[nodiscard]] constexpr auto operator""_c(){
int weight =std::pow(10, sizeof... (Cs));
// unused, would like to transform it using lambda that mutably captures
// weight
using ints = index_sequence<sizeof... (Cs)>;
// ugly fold way
auto val = ((weight/=10,(int)(Cs-'0')*weight) + ...);
return val;
}
int main(){
std::cout << 0_c << std::endl;
std::cout << 00_c << std::endl;
std::cout << 01_c << std::endl;
std::cout << 123_c << std::endl;
}
此代码适用于简单的情况(正确性并不重要,例如负数),它只是一个示例,但代码看起来很难看,并且 clang 会发出多次修改权重的警告,所以我猜代码是错误的(未定义或未指定的行为)虽然它似乎工作......
现在我想知道有没有办法让我ints
用有状态的 lambda(修改权重)来转换我使用的(它来自 boost::mp11,但同样的东西存在于 std::)。所以我想将整数转移<0,1,2>
到类似的东西中<100,10,1>
我想这已经被问过了,但这很难搜索。
需要明确的是:运算符“”只是一个玩具问题,我真正的问题是关于用有状态的 lambda 映射整数序列的值。
另外,如果问题不清楚:我很高兴使用 boost mp11,但在文档中找不到任何内容。
解决方案
所以我想将 <0,1,2> 的整数转换成 <100,10,1> 之类的东西
首先,您可以转换std::index_sequence
为std::array
,然后像往常一样对其执行操作,最后再次转换std::array
为std::index_sequence
。
为了让有状态 lambda 在编译时工作,我们可以接受一个可以返回有状态 lambda 的函数,然后在内部获取它:
template<std::size_t... Is>
constexpr auto transform_seq(std::index_sequence<Is...>, auto get_op) {
// index_sequence -> array
constexpr auto arr = [op = get_op()]() mutable {
std::array<std::size_t, sizeof...(Is)> arr{Is...};
for (auto& value : arr)
value = op(value);
return arr;
}();
// array -> index_sequence
constexpr auto seq = [&]<std::size_t... Js>(std::index_sequence<Js...>) {
return std::index_sequence<std::get<Js>(arr)...>{};
}(std::make_index_sequence<arr.size()>{});
return seq;
};
然后你可以index_sequence
根据op
你传入的进行转换:
using input1 = std::index_sequence<0,1,2>;
auto gen_op1 = [] {
return [w = 1000](auto x) mutable { w /= 10; return w; };
};
using res1 = decltype(transform_seq(input1{}, gen_op1));
static_assert(std::same_as<res1, std::index_sequence<100, 10, 1>>);
using input2 = std::index_sequence<0,1,2,3>;
auto gen_op2 = [] {
return [b = true] (auto x) mutable { b = !b; return b * 10 + x; };
};
using res2 = decltype(transform_seq(input2{}, gen_op2));
static_assert(std::same_as<res2, std::index_sequence<0,11,2,13>>);
推荐阅读
- javascript - 如何使用 jQuery 隐藏选定选项的某些文本值
- laravel - 如何使用数据透视表十月 Cms 确定结果范围
- grafana - Telegraf inputs.tail 与 zimbra.log
- powershell - 使用 selenium 和 powershell 时出错
- html - 使用 `iframe` 显示 LinkedIn 内容
- excel - 如何循环浏览所有工作表并根据活动工作表单元格值重命名?
- javascript - 如何使用 JavaSCript 进行惰性正则表达式 matchAll?
- google-apps-script - 如何仅使用 Google 脚本用粘贴值覆盖单元格函数
- r - 使用 lubridate 更改日期格式和计算持续时间
- macos - 在 macOS 上使用 Conda 安装 TensorFlow 2.2