这是 std::expected 的正确便捷包装吗?

dw2*_*192 3 c++ std std-expected

如果我正在编写一个返回对象的函数std::expected,并可能调用其他返回std::expected对象的函数,我发现自己编写这样的代码片段非常常见。

struct Foo {  };
std::expected<Foo, std::string> f();

auto res = f();
if(!res) return std::unexpected { res.error() };
auto val = res.value();
// do something with val
Run Code Online (Sandbox Code Playgroud)

所以我编写了一个这样的宏,在成功时“返回”值,在失败时“返回”错误。

#define CHECK(expr)\
({\
auto res = expr;\
if(!res) return std::unexpected { res.error() };\
res.value();\
})
Run Code Online (Sandbox Code Playgroud)

然后我可以这样使用它:

Foo foo = CHECK(f());
Run Code Online (Sandbox Code Playgroud)

我假设内部作用域中变量的生命周期应该与赋值表达式一样长。它是否正确?有没有什么情况可能会出错?

Pat*_*rts 6

使用这个宏,您可以编写如下函数:

std::expected<Qux, std::string> g() {
  Foo foo = CHECK(f());
  Bar bar = CHECK(b(foo));
  return q(bar);
}
Run Code Online (Sandbox Code Playgroud)
  • 无法从此模式推断出返回类型
  • 了解此代码的控制流需要了解(并记住)宏扩展为什么

我认为避免这种模式正是一元方法的std::expected<T,E>::and_then目的:

auto g() {
  return f()
      .and_then([](auto foo) { return b(foo); })
      .and_then([](auto bar) { return q(bar); });
}
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下,它还可以进一步缩短:

auto g() {
  return f()
      .and_then(b)
      .and_then(q);
}
Run Code Online (Sandbox Code Playgroud)

尽管实际上,我认为写出 lambda 表达式在实际代码中是更常见的情况。

编译器资源管理器:https://godbolt.org/z/vovTYfxf4