c++ - 将自动模板参数与 std::optional 结合,可能吗?
问题描述
我真的很喜欢 C++17 的自动模板参数,因为我不必为了使用非类型模板参数(例如带有转发参数的函数)而跳槽。
但这让我开始思考,std::optional
在转发函数没有有效结果的情况下,是否可以将它与其他类型(例如 )结合使用。例如:
#include <iostream>
#include <optional>
template <auto Func, typename E, typename ...Args>
auto safeCaller(Args&& ...args)
{
// this could even be wrapped in a loop for retrying
try
{
return Func(std::forward<Args>(args)...);
}
catch (E &e)
{
// ... perform some logging perhaps? or whatever else is relevant
return std::nullopt;
}
}
int foo(std::string bar)
{
return bar.size();
}
int main()
{
// specialise safeCaller to work on foo and to expect std::runtime_error
auto result = safeCaller<foo, std::runtime_error>("baz");
if (result)
{
std::cout << *result << std::endl;
}
return 0;
}
目前这有几个问题:
main.cpp: In instantiation of ‘auto safeCaller(Args&& ...) [with auto Func = foo; E = std::runtime_error; Args = {const char (&)[4]}]’:
main.cpp:25:60: required from here
main.cpp:14:21: error: inconsistent deduction for auto return type: ‘int’ and then ‘std::nullopt_t’
14 | return std::nullopt;
| ^~~~~~~
main.cpp:14:21: error: ‘struct std::nullopt_t’ used where a ‘int’ was expected
main.cpp: In function ‘int main()’:
main.cpp:28:23: error: invalid type argument of unary ‘*’ (have ‘int’)
28 | std::cout << *result << std::endl;
这是一个玩具示例,但我希望有一些东西可以作为函数/调用的装饰器,这些函数/调用可能有助于一些通用的异常处理、清理和/或日志记录。
我愿意接受替代方案,std::optional
只要有一种方法可以传达呼叫无法完成,因此不会返回任何结果。
解决方案
std::optional
你可以从传入的函数中推断出你想要什么类型。
然后,如果它抛出,您可以返回一个空的可选项。
#include <iostream>
#include <optional>
template <auto Func, typename E, typename ...Args>
auto safeCaller(Args&& ...args)
{
using ret = std::optional<decltype(Func(std::forward<Args>(args)...))>;
// this could even be wrapped in a loop for retrying
try
{
return ret{Func(std::forward<Args>(args)...)};
}
catch (E &e)
{
// ... perform some logging perhaps? or whatever else is relevant
return ret{};
}
}
int foo(std::string bar)
{
return bar.size();
}
int main()
{
// specialise safeCaller to work on foo and to expect std::runtime_error
auto result = safeCaller<foo, std::runtime_error>("baz");
if (result)
{
std::cout << *result << std::endl;
}
return 0;
}
推荐阅读
- mysql - 如何执行 SQL 查询,该查询将根据 id 从 2 个表中返回组合数据
- r - 在 r 中使用多个 3D 数组和 Apply 函数
- python - 在numpy中索引行和列
- excel - 将列范围导出到 PDF 页面
- python - 如何在python中使用opencv检测半圆形中心?
- c# - 如何从发布 ASP.NET CORE MVC 中检索用户信息
- node.js - 在视图目录“views”中查找视图“..”失败?
- java - 读取和比较两个大文件
- oracle - Oracle ORA -932 预期数字得到 CHAR
- discord.py - 我在 discord.py 中搜索货币代码有什么问题