我真的很喜欢 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
只要有一种方法可以传达呼叫无法完成,因此不会返回任何结果。