c++ - 使用可变参数模板化帮助器的多个 std::variant 访问
问题描述
我正在尝试制作一个有助于处理 Nstd::variant
类型的函数。
注意:我正在尝试验证所有路径的编译时间。所以对我std::optional
来说std::holds_alternative
是不可行的。
实现如下:
template<typename T>
using Possible = std::variant<std::monostate, T>;
template<typename... Types>
void ifAll(std::function<void(Types...)> all, Possible<Types>&&... possibles)
{
std::visit(
[&](auto&&... args) {
if constexpr ((... &&
std::is_same_v<std::decay_t<decltype(args)>, Types>))
{
return all(std::forward<Types>(args)...);
}
else
{
std::cout << "At least one type is monostate" << std::endl;
}
},
possibles...);
}
使用该功能的一个例子是:
int main()
{
Possible<int> a = 16;
Possible<bool> b = true;
ifAll([](const int& x, const bool& y)
-> void { std::cout << "All types set!" << std::endl; },
a,
b);
}
但是我得到一个编译器错误:
TestFile.cc: error: no matching function for call to 'ifAll'
ifAll([](const int& x, const bool& y)
^~~~~
TestFile.cc: note: candidate template ignored: could not match
'function<void (type-parameter-0-0...)>' against '(lambda at
TestFile.cc)'
void ifAll(std::function<void(Types...)> all, Possible<Types>&&... possibles)
^
为什么我提供的 lambda 与函数签名不匹配?
尝试修复 1
我尝试搬进去a
,b
但仍然无法正常工作:
ifAll([](const int& x, const bool& y)
-> void { std::cout << "All types set!" << std::endl; },
std::move(a),
std::move(b));
解决方案
以下电话将起作用:
int main() {
Possible<int> a = 16;
Possible<bool> b = true;
std::function<void(int, bool)> fun = [](int x, bool y) -> void {
std::cout << "All types set!" << std::endl;
};
ifAll(fun,
std::move(a),
std::move(b));
}
或将您的函数签名切换为:
template <typename... Types>
void ifAll(std::function<void(Types...)> const& all, Possible<Types>&... possibles)
然后你可以在没有的情况下调用它std::move
:
int main() {
Possible<int> a = 16;
Possible<bool> b = true;
std::function<void(int, bool)> fun = [](int x, bool y) -> void {
std::cout << "All types set!" << std::endl;
};
ifAll(fun, a, b);
}
推荐阅读
- java - 使用单车或工具 CVRP 重新加载
- rest - OpenAPI 3.0 规范中哪里允许使用 $ref?
- c# - 有没有办法让布局元素自动重新缩放而不是自动调整大小?
- apollo - 在生产模式下是否有任何选项可以禁用游乐场?
- jquery - UI 不显示列的标签(数据类型:文本)
- python - 为什么整数的 Python sum() 和 np.sum() 不同?
- c# - 是否可以为 C# 应用程序分配某些所需的内存和 CPU,以便在 CPU 使用率有时为 100% 时不会冻结?
- git - GitHub 桌面上传限制
- spring-boot - spring security - 发送http请求而不是https
- c# - Xamarin 在 Android 项目中打开内容页面