快捷搜索:  汽车  科技

c语言const关键字应用场合(c11std:function)

c语言const关键字应用场合(c11std:function)个人浅解:std::function满足CopyConstructible和CopyAssignable的要求。std::function satisfies the requirements of CopyConstructible and CopyAssignable.类模板std::function是一种通用的多态函数包装器。std::function可以存储,复制和调用任何Callable 目标的实例- 函数,lambda表达式,绑定表达式或其他函数对象,以及指向成员函数和指向数据成员的指针。所存储的可调用对象被称为目标的std::function。如果a不std::function包含目标,则将其称为空。调用目标的的空std::function的结果的std :: bad_function_call抛出异常。

c语言const关键字应用场合(c11std:function)(1)

这是c 11新添加的 头文件#include <functional>

官方说明:

Class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store copy and invoke any Callable target -- functions lambda expressions bind expressions or other function objects as well as pointers to member functions and pointers to data members.

The stored callable object is called the target of std::function. If a std::function contains no target it is called empty. Invoking the target of an empty std::function results in std::bad_function_call exception being thrown.

std::function satisfies the requirements of CopyConstructible and CopyAssignable.

类模板std::function是一种通用的多态函数包装器。std::function可以存储,复制和调用任何Callable 目标的实例- 函数,lambda表达式,绑定表达式或其他函数对象,以及指向成员函数和指向数据成员的指针。

所存储的可调用对象被称为目标的std::function。如果a不std::function包含目标,则将其称为空。调用目标的的空std::function的结果的std :: bad_function_call抛出异常。

std::function满足CopyConstructible和CopyAssignable的要求。

个人浅解:

将其当做std::string 、std::vector<> 、这样的类型就可以了。只不过其值为函数指针,但比函数指针更灵活。

因为std::function 是一种模板,所以要传入类型,就如std::vector<int> 传入类型int一样

不过 std::function传入的是函数类型 返回值 (参数类型) 如:std::function<void (int)>

示例1 : 普通函数

void gFunc() { cout << "gFunc" << endl; } int main() { std::function<void()> f = gFunc; f(); getchar(); return 0; }

c语言const关键字应用场合(c11std:function)(2)

示例2 模板函数

template <class T> T g_Add(T i T j) { cout << i j; return i j; } int main() { std::function<int(int int)> f = g_Add<int>; f(2 3); getchar(); return 0; }

c语言const关键字应用场合(c11std:function)(3)

示例三: 匿名函数

auto g_Lambda = [](int i int j) { return i j; }; //匿名函数 此处有分号 int main() { std::function<int(int int)> f = g_Lambda; cout<<f(2 3); getchar(); return 0; }

c语言const关键字应用场合(c11std:function)(4)

示例四:函数对象

/函数对象 struct Add { int operator()(int i int j) { return i j; } }; //模板函数对象 template <class T> struct AddT { T operator()(T i T j) { return i j; } }; int main() { std::function<int(int int)> f = Add(); cout<<f(2 3)<<endl; std::function<int(int int)> ft = AddT<int>(); cout << ft(5 6)<<endl; getchar(); return 0; }

c语言const关键字应用场合(c11std:function)(5)

示例5:类成员函数

class Computer { public: static int Add(int i int j) { return i j; } template<class T> static T AddT(T i T j) { return i j; } int AddN(int i int j) { return i j; } }; //存储对成员函数的调用 int main() { //1、 类静态函数 std::function<int(int int)> f = &Computer::Add; cout << f(1 1) << endl; //2、 类静态模板函数 std::function<int(int int)> ft = &Computer::AddT<int>; cout << ft(1 1) << endl; //普通函数绑定 需要构造类对象 Computer c; //3、 普通函数 需使用bind 将类对象地址 &c 绑定上 std::function<int(int int)> fN = std::bind(&Computer::AddN &c placeholders::_1 placeholders::_2); cout << fN(1 1) << endl; //4、普通函数, 也可以这样调用 个人觉得这个比 bind 麻烦,不建议 std::function <int(const Computer & int int)> fN2 = &Computer::AddN; cout << fN2(c 1 1) << endl; getchar(); return 0; }

c语言const关键字应用场合(c11std:function)(6)

猜您喜欢: