c++ - 何时使用愚蠢的单位类型

标签 c++ folly

在下面的代码片段中,我看到了这种Unit类型,但无法确定何时使用它以及它在做什么?我阅读了https://github.com/facebook/folly/blob/master/folly/Unit.h,但仍然不知道如何在程序中使用此Unit。单位可以提供哪些典型方案?

   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;
      });

最佳答案

这直接来自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.

关于c++ - 何时使用愚蠢的单位类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59937064/

相关文章:

c++ - 链接到 jemalloc 的 SDL2 程序在 Mac os x 但不是 ubuntu 上崩溃

c++ - Facebook folly::AccessSpreader 是如何工作的?

c++ - 无法构建 surround360_render - CMake 问题

C++ CRTP 类层次结构

c++ - OpenCV:查找轮廓(); - 我使用的是什么内核

c++ - 逐个字母地存储一个字符串并打印它

python - 编译支持GPU的Tensorflow时,遇到CUDA_TOOLKIT_PATH未绑定(bind)变量

c++ - 使用MYSQL Server 5.5完成端到端教程C++