首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >何时使用folly单元类型

何时使用folly单元类型
EN

Stack Overflow用户
提问于 2020-01-28 02:44:24
回答 2查看 675关注 0票数 1

在下面的代码片段中,我看到了这个Unit类型,但是不知道什么时候使用它,它在做什么?我读了这个https://github.com/facebook/folly/blob/master/folly/Unit.h,但仍然不知道如何在我的程序中使用这个单元。单元将提供帮助的典型场景有哪些?

代码语言:javascript
复制
   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;
      });
EN

回答 2

Stack Overflow用户

发布于 2020-01-28 03:05:41

这直接来自于comments on the class itself,并解释了几乎所有的事情,包括一个用例。

代码语言:javascript
复制
/// 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.
票数 2
EN

Stack Overflow用户

发布于 2020-12-01 19:49:39

folly::Unit类提供了一个标准typename伪类,它是一个真实类型(不像),但它本身是无用的,您可以在实例化预先存在的模板(如folly::Future)时将其用作参数,并且该模板的实例化不需要或不使用与模板的一个或多个typename参数相对应的对象。当函数模板参数用作函数的返回类型,但函数的实例化将具有void返回值时,标准的伪类型(如folly:Unit )尤其有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59937064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档