我建模了一个返回实例的函数std::expected<void, Error>
- 我告诉自己“我可以使用新标准,因此我将据此设计我的库” - 并且我非常肯定有良好而具体的错误处理。现在事实证明,所有单子操作都只适用std::expected
于非 void 项。即使 void 有专门化,单子操作也不可用。我理解or_else
需要返回值 - 但是 void 有专门化,那么为什么这不起作用呢?
std::expected<void, Error> fun (int);
fun (19).and_then ([]() { doSomething(); }).or_else ([] (Error e) { std::println ("Uh oh error.."); });
得出:
/usr/include/c++/14.2.1/expected:1586:37: error: static assertion failed
1586 | static_assert(__expected::__is_expected<_Up>);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14.2.1/expected:1586:37: note: ‘std::__expected::__is_expected<void>’ evaluates to false
/usr/include/c++/14.2.1/expected:1587:25: error: ‘std::remove_cvref<void>::type’ {aka ‘void’} is not a class, struct, or union type
1587 | static_assert(is_same_v<typename _Up::error_type, _Er>);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14.2.1/expected:1592:20: error: expression list treated as compound expression in functional cast [-fpermissive]
1592 | return _Up(unexpect, std::move(_M_unex));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/david/tests/cpp/src/expect.cpp: In function ‘int main()’:
/home/david/tests/cpp/src/expect.cpp:49:26: error: invalid use of ‘void’
这里有什么问题?这个功能只完成了一半(因此标准不完整)还是我误解了它的意义?