Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 何时使用folly单位类型_C++_Folly - Fatal编程技术网

C++ 何时使用folly单位类型

C++ 何时使用folly单位类型,c++,folly,C++,Folly,在下面的代码片段中,我看到了这个单元类型,但不知道何时使用它以及它在做什么?我读过这篇文章,但仍然不知道如何在我的程序中使用这个单元。单元将提供哪些典型场景帮助 Future<Unit> fut3 = std::move(fut2) .thenValue([](string str) { cout << str << endl; }) .thenTry([](folly::Try<string&g

在下面的代码片段中,我看到了这个
单元
类型,但不知道何时使用它以及它在做什么?我读过这篇文章,但仍然不知道如何在我的程序中使用这个单元。单元将提供哪些典型场景帮助

   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;
      });
Future fut3=std::move(fut2)
.thenValue([](字符串str){

cout这直接来自于,并且解释了几乎所有的事情,包括用例

/// 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.
///在函数式编程中,退化情况通常称为“unit”
//C++,“空洞”通常是最好的类比。
///对于空隙,需要特殊套管,这通常是模板的责任
///元编程。因此,与其编写专门化来处理类似这样的情况
///在某些容器中,库作者可能会简单地排除这一点
///让库用户使用SomeContainer。包含的值可能会被忽略。
///容易多了。
///
///“void”是一种完全不允许值的类型。不可能
///构造此类型的值。
///“单位”是只允许一个唯一值的类型。它是
///可以构造此类型的值,但它始终是相同的值
///每一次,所以它是无趣的。

folly::Unit类提供了一个标准的伪类,它是一个实类型(与void不同),但它本身是无用的,在实例化预先存在的模板(如folly::Future)时,可以将其用作参数并且该模板的实例化不需要或不使用与模板的一个或多个typename参数相对应的对象。当函数模板参数用作函数的返回类型,但函数的实例化将具有v返回值。< / P>你从C++的文档中发现了什么?我不知道如何使用它。你能给我几个例子吗?它可以使用。单元工作得像空洞一样。除了C++的差异之外,这感觉就像是复制品。这能回答你的问题吗?标准是现代C++。从C++17开始的nit类型。在C++17之前(现在仍然存在),许多人发现需要某种类型的单位类型,就自己做了。投了反对票,因为这篇文章没有回答OP的问题,“何时使用愚蠢的单位类型”,它只是引用了一些没有解释何时使用该类的文档,甚至没有以OP能够理解的方式解释该类。