日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++11中std::function的使用

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11中std::function的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

類模版std::function是一種通用、多態(tài)的函數(shù)封裝。std::function的實(shí)例可以對任何可以調(diào)用的目標(biāo)實(shí)體進(jìn)行存儲、復(fù)制、和調(diào)用操作,這些目標(biāo)實(shí)體包括普通函數(shù)、Lambda表達(dá)式、函數(shù)指針、以及其它函數(shù)對象等

通過std::function對C++中各種可調(diào)用實(shí)體(普通函數(shù)、Lambda表達(dá)式、函數(shù)指針、以及其它函數(shù)對象等)的封裝,形成一個(gè)新的可調(diào)用的std::function對象,讓我們不再糾結(jié)那么多的可調(diào)用實(shí)體。

std::function實(shí)現(xiàn)了一套類型消除機(jī)制,可以統(tǒng)一處理不同的函數(shù)對象類型。以前使用函數(shù)指針來完成這些,現(xiàn)在可以使用更安全的std::function來完成這些任務(wù)

C++11中推出std::function是為了泛化函數(shù)對象,函數(shù)指針,引用函數(shù),成員函數(shù)的指針,讓我們可以按更統(tǒng)一的方式寫出更加泛化的代碼。

std::function對象是對C++中現(xiàn)有的可調(diào)用實(shí)體的一種類型安全的包裹(像函數(shù)指針這類可調(diào)用實(shí)體,是類型不安全的)。

可調(diào)用實(shí)體轉(zhuǎn)換為std::function對象需要遵守以下兩條原則:

(1)、std::function對象的參數(shù)能轉(zhuǎn)換為可調(diào)用實(shí)體的參數(shù);

(2)、可調(diào)用實(shí)體的返回值能轉(zhuǎn)換為std::function對象的返回值(注意,所有的可調(diào)用實(shí)體的返回值都與返回void的std::function對象的返回值兼容)。

在C++中,”可調(diào)用對象”概念:(1)、是一個(gè)函數(shù)指針;(2)、是一個(gè)具有operator()成員函數(shù)的類對象(仿函數(shù));(3)、是一個(gè)可被轉(zhuǎn)換為函數(shù)指針的類對象;(4)、是一個(gè)類成員(函數(shù))指針。

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.

There are two performance implications of using std::function that might surprise you:

(1)、When calling a std::function, it does a virtual function call.

(2)、When assigning a lambda with significant captures to a std::function, it will do a dynamic memory allocation.

下面是從其他文章中copy的測試代碼,詳細(xì)內(nèi)容介紹可以參考對應(yīng)的reference:

#include "function.hpp"
#include <iostream>
#include <string>
#include <functional>
#include <vector>///
// reference: http://en.cppreference.com/w/cpp/utility/functional/function
struct Foo {Foo(int num) : num_(num) {}void print_add(int i) const { std::cout << num_ + i << '\n'; }int num_;
};void print_num(int i)
{std::cout << i << '\n';
}struct PrintNum {void operator()(int i) const{std::cout << i << '\n';}
};int test_function1()
{// store a free functionstd::function<void(int)> f_display = print_num;f_display(-9);// store a lambdastd::function<void()> f_display_42 = []() { print_num(42); };f_display_42();// store the result of a call to std::bindstd::function<void()> f_display_31337 = std::bind(print_num, 31337);f_display_31337();// store a call to a member function//std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;const Foo foo(314159);//f_add_display(foo, 1);// store a call to a data member accessor//std::function<int(Foo const&)> f_num = &Foo::num_;//std::cout << "num_: " << f_num(foo) << '\n';// store a call to a member function and objectusing std::placeholders::_1;std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1);f_add_display2(2);// store a call to a member function and object ptrstd::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1);f_add_display3(3);// store a call to a function objectstd::function<void(int)> f_display_obj = PrintNum();f_display_obj(18);return 0;
}///
// reference: https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/
void execute(const std::vector<std::function<void()>>& fs)
{for (auto& f : fs)f();
}void plain_old_func()
{std::cout << "I'm an old plain function" << std::endl;
}class functor
{
public:void operator()() const{std::cout << "I'm a functor" << std::endl;}
};int test_function2()
{std::vector<std::function<void()>> x;x.push_back(plain_old_func);functor functor_instance;x.push_back(functor_instance);x.push_back([](){std::cout << "HI, I'm a lambda expression" << std::endl;});execute(x);return 0;
}///
// reference: http://shaharmike.com/cpp/lambdas-and-functions/
void global_f() {std::cout << "global_f()" << std::endl;
}struct Functor {void operator()() { std::cout << "Functor" << std::endl; }
};int test_function3()
{std::function<void()> f;std::cout << "sizeof(f) == " << sizeof(f) << std::endl;f = global_f;f();f = [](){ std::cout << "Lambda" << std::endl; };f();Functor functor;f = functor;f();return 0;
}

GitHub:https://github.com/fengbingchun/Messy_Test

總結(jié)

以上是生活随笔為你收集整理的C++11中std::function的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

歡迎分享!

轉(zhuǎn)載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:C++11中std::function的使用