- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想访问一个“递归” std::variant
使用 lambda 和重载创建函数(例如 boost::hana::overload
)。
假设我有一个名为 my_variant
的变体类型可以存储一个int
, 一个 float
或 vector<my_variant>
:
struct my_variant_wrapper;
using my_variant =
std::variant<int, float, std::vector<my_variant_wrapper>>;
struct my_variant_wrapper
{
my_variant _v;
};
(我使用包装器 my_variant_wrapper
类来递归定义变体类型。)
我想递归访问根据存储类型打印不同内容的变体。这是一个 working example使用 struct
基于访问者:
struct struct_visitor
{
void operator()(int x) const { std::cout << x << "i\n"; }
void operator()(float x) const { std::cout << x << "f\n"; }
void operator()(const std::vector<my_variant_wrapper>& x) const
{
for(const auto& y : x) std::visit(*this, y._v);
}
};
调用 std::visit
使用上述访问者打印所需的输出:
my_variant v{
std::vector<my_variant_wrapper>{
my_variant_wrapper{45},
std::vector<my_variant_wrapper>{
my_variant_wrapper{1}, my_variant_wrapper{2}
},
my_variant_wrapper{33.f}
}
};
std::visit(struct_visitor{}, v);
// Prints:
/*
45i
1i
2i
33f
*/
我想使用 boost::hana::overload
在本地将访问者创建为一系列重载的 lambda 表达式和 boost::hana::fix
.
fix
是 Y-combinator 的一个实现,可用于在类型推导的 lambda 中实现递归。 (有关更多信息,请参阅 this question。)
这是我尝试过的,预计会奏效:
namespace bh = boost::hana;
auto lambda_visitor = bh::fix([](auto self, const auto& x)
{
bh::overload(
[](int y){ std::cout << y << "i\n"; },
[](float y){ std::cout << y << "f\n"; },
[&self](const std::vector<my_variant_wrapper>& y)
{
for(const auto& z : y) std::visit(self, z._v);
})(x);
});
我的推理如下:
boost::hana::fix
返回一个一元泛型 lambda,可用作 std::variant
的访问者.
boost::hana::fix
采用二进制通用 lambda,其中第一个参数是允许 lambda 递归的一元函数,第二个参数是 lambda 主体的初始参数。
调用 boost::hana::overload
在 my_variant
中包含所有可能类型的处理程序创建某种访客,相当于 struct_visitor
.
使用 self
而不是 lambda_visitor
在const std::vector<my_variant_wrapper>&
里面重载应该允许递归正常工作。
立即使用 bh::overload(...)(x)
调用创建的重载应该触发递归访问。
不幸的是,as you can see in this wandbox example , lambda_visitor
示例无法编译,喷出大量几乎无法辨认的模板错误:
...
/usr/local/boost-1.61.0/include/boost/hana/functional/fix.hpp:74:50: error: use of 'main():: [with auto:2 = boost::hana::fix_t >; auto:3 = int]' before deduction of 'auto' { return f(fix(f), static_cast(x)...); }
...
该错误似乎与我在不使用 boost::hana::fix
时得到的错误类似:
auto lambda_visitor = bh::overload(
[](int y){ std::cout << y << "i\n"; },
[](float y){ std::cout << y << "f\n"; },
[](const std::vector<my_variant_wrapper>& y)
{
for(const auto& z : y) std::visit(lambda_visitor, z._v);
});
std::visit(lambda_visitor, v);
error: use of 'lambda_visitor' before deduction of 'auto' for(const auto& z : y) std::visit(lambda_visitor, z._v);
我做错了什么?是否有可能使用 fix
实现本地递归变体访问 , overload
和一组 lambda?
我的直觉是 lambda_visitor
本来“等同于” struct_visitor
, 感谢 fix
提供的间接寻址.
最佳答案
让我们选择一个更简单的例子。我们想使用定点组合子来实现 gcd
。首先可能是这样的:
auto gcd = bh::fix([](auto self, int a, int b) {
return b == 0 ? a : self(b, a%b);
});
std::cout << gcd(12, 18);
这无法使用 gcc 编译,最终会产生此错误:
/usr/local/boost-1.61.0/include/boost/hana/functional/fix.hpp:74:50: error: use of 'main()::<lambda(auto:2, int, int)> [with auto:2 = boost::hana::fix_t<main()::<lambda(auto:2, int, int)> >]' before deduction of 'auto'
{ return f(fix(f), static_cast<X&&>(x)...); }
^
我们传递给 fix()
的 lambda 具有推导的返回类型。但是我们如何推断呢?只有一个 return 语句,而且是递归的!我们需要给编译器一些帮助。要么我们需要分解我们的 return
语句,以便有一个清晰的类型:
auto gcd = bh::fix([](auto self, int a, int b) {
if (b == 0) {
return a;
}
else {
return self(b, a%b);
}
});
或者简单地明确提供返回类型:
auto gcd = bh::fix([](auto self, int a, int b) -> int {
return b == 0 ? a : self(b, a%b);
});
这两个选项都可以编译和工作。
您的原始示例也是如此。如果您只是指定 lambda 返回 void
,一切正常:
auto lambda_visitor = bh::fix([](auto self, const auto& x) -> void
// ^^^^^^^^
{
bh::overload(
[](int y){ std::cout << y << "i\n"; },
[](float y){ std::cout << y << "f\n"; },
[&self](const std::vector<my_variant_wrapper>& y)
{
for(const auto& z : y) std::visit(self, z._v);
})(x);
});
std::visit(lambda_visitor, v);
关于c++ - 使用 lambda 和定点组合器递归访问 `std::variant`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819600/
我创建了一个简单的 Variant 类来存储字符串、整数、 double 等。我正在尝试使用 std::map 类型的映射但我收到了这个奇怪的错误: In file included from /us
我有一个整数数组来检索 std::variant 中的内容。但是编译失败,报错No matching function to call 'get' .您能否解释原因,并提出实现相同目的的可行解决方案?
我的问题涉及 boost::variant 转换与 C++ 中的 std::vector 的混合。在我的项目中,我使用变体作为 SQL 的输出。我将始终只使用一种类型的变体。我想要做的是从变量、变量
(警告:虽然它乍一看可能是一个问题,但这是 而不是 一个初学者级别的问题。如果您熟悉“让强制”这个短语或者您曾经查看过 VBA 规范,请继续阅读。) 假设我有一个 Variant 类型的表达式,我想将
我正在使用一个相当笨拙的 c 接口(interface)来存储集合。 LowLevelStorer 类表示我为此接口(interface)编写的包装器。 Storer 类是一个高级类,它与Data 有
我试图将两个变体组合成一个变体只是为了便于阅读。这是代码: using VariantType_basic = std::variant; using VariantType_vector = std
给定一个类型为 std::variant 的变量,我检查过它不包含C .如何将其转换为 std::variant ? std::variant convert(std::variant value)
使用boost:variant: #include #include #include template boost::variant _tuple_index(size_t i, const
在 answer 中对于这个 SO 问题: What is the equivalent of boost::variant in the C++ standard library? 提到boost:
自从在 Android Gradle 插件 0.13.0 中升级到 gradle 2.1 后,这个问题就出现了,但我一直无法理解为什么有时会记录此警告。 考虑此 block 以根据变体类型重命名 AP
我想为变量实现一个模板方法。 但是根据输入是否为 int 变量(char、short、int),我想处理它与输入为 float 变量(float、double、long double)的情况有所不同。
boost::variant通过 boost::variant<>::types 公开其变体类型列表, 可以方便地与 boost::mpl::for_each 一起使用. std::variant缺少
歌词: 我尝试通过 MPI 实现任务池。所以我需要某种 RPC,但它可以在我的程序的不同部分之间工作,这意味着处理器 A 希望处理器 B 以参数 D 调用函数 C。我们不能像处理线程那样在进程之间传递
目前我的库使用 boost::optional 和 boost::variant。由于 C++17 已经发布,我想添加一个选项,它可以与 boost 和 std 一起使用。 所以我成功地测试了带有 b
假设我有: class TypeA { }; class TypeB { }; typedef boost::variant Type; 没关系: void foo(Type t) { }; int
假设我有一个嵌套的 boost::variant -类型TNested包含一些类型和一些其他 boost::variant类型(它本身不能再次包含 boost::variant types ,因此不会
使用带有 gradle 插件版本 3.3.0-alpha11 的 Android Studio 3.3 Canary 11。尝试同步 gradle 时会抛出以下错误 WARNING: API 'var
我看过这篇关于使用 std::variant 的文章.这是因为以下代码引发了代码分析警告: void CChristianLifeMinistryHtmlView::OnTimer(UINT_PTR
我有一个 Delphi 6 类对象,其中包含 30 个变体的数组,每个变体都通过不同的索引属性公开。例如: property responseCode: integer Index 7
...或在内部快速更改类型 std::variant在源代码中。 下面是列表本身及其容器元素的头文件中的代码。 // HVector.hh class HVector: public std::vec
我是一名优秀的程序员,十分优秀!