std :: function的用法和语法 [英] Usage and Syntax of std::function

查看:97
本文介绍了std :: function的用法和语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必要使用 std :: function ,但我不知道以下语法的含义。

It is necessary to me to use std::function but I don't know what the following syntax means.

std::function<void()> f_name = []() { FNAME(); };

使用 std :: function

推荐答案

std :: function 是类型擦除对象。这意味着它将删除某些操作如何发生的细节,并为它们提供统一的运行时界面。对于 std :: function ,主要的 1 操作是使用 operator复制/移动,销毁和调用的( )-'函数,如呼叫操作符'。

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function, the primary1 operations are copy/move, destruction, and 'invocation' with operator() -- the 'function like call operator'.

用不太深刻的英语来说,这意味着 std :: function 可以包含几乎任何在调用时都像函数指针一样的对象。

In less abstruse English, it means that std::function can contain almost any object that acts like a function pointer in how you call it.

它支持的签名位于尖括号内: std: :function< void()> 接受零参数,不返回任何内容。 std :: function< double(int,int)> 接受两个 int 参数并返回 double 。通常, std :: function 支持存储任何函数式对象,该对象的参数可以从其参数列表中转换,并且其返回值可以转换为其返回值。

The signature it supports goes inside the angle brackets: std::function<void()> takes zero arguments and returns nothing. std::function< double( int, int ) > takes two int arguments and returns double. In general, std::function supports storing any function-like object whose arguments can be converted-from its argument list, and whose return value can be converted-to its return value.

重要的是要知道 std :: function 和lambda是不同的(如果兼容)野兽。

It is important to know that std::function and lambdas are different, if compatible, beasts.

该行的下一部分是lambda。这是C ++ 11中的新语法,增加了编写类似于函数的对象的功能,这些对象可以使用()进行调用。可以删除此类对象的类型并将其存储在 std :: function 中,但会花费一些运行时间。

The next part of the line is a lambda. This is new syntax in C++11 to add the ability to write simple function-like objects -- objects that can be invoked with (). Such objects can be type erased and stored in a std::function at the cost of some run time overhead.

[](){代码} 特别是一个非常简单的lambda。与此相对应:

[](){ code } in particular is a really simple lambda. It corresponds to this:

struct some_anonymous_type {
  some_anonymous_type() {}
  void operator()const{
    code
  }
};

上述简单伪函数类型的实例。像上面这样的实际类是发明的。由编译器以实现定义的唯一名称(通常包括用户定义类型不能包含的符号)实现(我不知道您是否有可能在不发明此类的情况下遵循该标准,但是我知道每个编译器

an instance of the above simple pseudo-function type. An actual class like the above is "invented" by the compiler, with an implementation defined unique name (often including symbols that no user-defined type can contain) (I do not know if it is possible that you can follow the standard without inventing such a class, but every compiler I know of actually creates the class).

完整的lambda语法如下:

The full lambda syntax looks like:

[ capture_list ]( argument_list )
-> return_type optional_mutable
{
  code
}

但是很多部分可以省略或保留空的。 capture_list对应于结果匿名类型的构造函数及其成员变量,arguments_list operator()的参数,返回类型为返回类型。在使用capture_list创建实例时,也神奇地调用了lambda实例的构造函数。

but many parts can be omitted or left empty. The capture_list corresponds to both the constructor of the resulting anonymous type and its member variables, the argument_list the arguments of the operator(), and the return type the return type. The lambda instance's constructor is also magically called when the instance is created with the capture_list.

[ capture_list ]( argument_list ) -> return_type { code }

基本上变成

struct some_anonymous_type {
  // capture_list turned into member variables
  some_anonymous_type( /* capture_list turned into arguments */ ):
    /* member variables initialized */
  {}
  return_type operator()( argument_list ) const {
    code
  }
};

请注意,在模板参数(已添加到lambda中,但未在上面提及)。

Note that in c++20 template arguments where added to lambdas, and that isn't covered above.

[]<typename T>( std::vector<T> const& v ) { return v.size(); }




1 此外,还存储了RTTI(typeid ),并且包含了强制转换为原始类型的操作。


1 In addition, RTTI is stored (typeid), and the cast-back-to-original-type operation is included.

这篇关于std :: function的用法和语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆