在下面的代码片段中,我看到了这个Unit类型,但是不知道什么时候使用它,它在做什么?我读了这个https://github.com/facebook/folly/blob/master/folly/Unit.h,但仍然不知道如何在我的程序中使用这个单元。单元将提供帮助的典型场景有哪些?
Future<Unit> fut3 = std::move(fut2)
.thenValue([](string str) {
cout << str << endl;
})
.thenTry([](folly::Try<string> strTry) {
cout << strTry.value() << endl;
})
.thenError(folly::tag_t<std::exception>{}, [](std::exception const& e) {
cerr << e.what() << endl;
});发布于 2020-01-28 03:05:41
这直接来自于comments on the class itself,并解释了几乎所有的事情,包括一个用例。
/// In functional programming, the degenerate case is often called "unit". In
/// C++, "void" is often the best analogue. However, because of the syntactic
/// special-casing required for void, it is frequently a liability for template
/// metaprogramming. So, instead of writing specializations to handle cases like
/// SomeContainer<void>, a library author may instead rule that out and simply
/// have library users use SomeContainer<Unit>. Contained values may be ignored.
/// Much easier.
///
/// "void" is the type that admits of no values at all. It is not possible to
/// construct a value of this type.
/// "unit" is the type that admits of precisely one unique value. It is
/// possible to construct a value of this type, but it is always the same value
/// every time, so it is uninteresting.发布于 2020-12-01 19:49:39
folly::Unit类提供了一个标准typename伪类,它是一个真实类型(不像),但它本身是无用的,您可以在实例化预先存在的模板(如folly::Future)时将其用作参数,并且该模板的实例化不需要或不使用与模板的一个或多个typename参数相对应的对象。当函数模板参数用作函数的返回类型,但函数的实例化将具有void返回值时,标准的伪类型(如folly:Unit )尤其有用。
https://stackoverflow.com/questions/59937064
复制相似问题