網站首頁 編程語言 正文
沒有捕獲任何變量的Lambda表達式可以轉換成與它的調用原型一致的函數指針。
參考下面的代碼
void example1()
{
? ? auto add = [](int x, int y)
? ? {
? ? ? ? return x + y;
? ? };
? ? int x = 2, y = 3;
? ? int z1 = add(x, y); ? ? ? ? ? ? ? ? ?// 調用Lambda
? ? int(*f)(int, int) = add; ? ? ? ? ? ? // Lambda轉換成函數指針
? ? int z2 = f(x, y); ? ? ? ? ? ? ? ? ? ?// 調用函數
? ? cout << z1 << ", " << z2 << endl;
}
Lambda是實現了函數調用運算符的匿名類(anonymous class)。對于每一個Lambda,編譯器創建匿名類,并定義相應的數據成員存儲Lambda捕獲的變量。沒有捕獲變量的Lambda不包含任何含成員變量。
一個沒有任何成員變量(包括沒有虛函數表指針)的類型,在空指針上調用成員函數也不會有任何的問題,因為它的成員函數不會通過this指針訪問內存。
當Lambda向函數指針的轉換時,編譯器為Lambda的匿名類實現函數指針類型轉換運算符。
上面的例子中,編譯器實現operator int(*)(int, int)。
下面是Visual C++編譯器為語句int(*f)(int, int) = add生成的64位匯編代碼:
lea ? ? ? ? rcx,[add] ?
call ? ? ? ?<lambda_0e153cdea67ea404383c23c1022dd325>::operator int (__cdecl*)(int,int)?
mov ? ? ? ? qword ptr [f],rax ?
第1行,變量"add"的地址存入rcx寄存器;第2行,調用匿名類的函數指針類型轉換運算符;第3行,返回結果存入變量f。
提示:在默認的成員函數調用約定__thiscall下,this指針通過rcx寄存器傳遞, 有關__thiscall的詳細內容,請參考:https://docs.microsoft.com/en-us/cpp/cpp/thiscall。
下面是類型轉換運算符實現的一行關鍵匯編代碼
lea ? ? ? ? rax,[<lambda_0e153cdea67ea404383c23c1022dd325>::<lambda_invoker_cdecl>]?
這一行把匿名類的名為lambda_invoker_cdecl的函數地址存入用于返回結果的寄存器rax。因為只有類的靜態函數可以轉換成非成員函數指針,所以lambda_invoker_cdecl是靜態函數。下面是此函數其中一段匯編代碼:
xor ? ? ? ? rcx,rcx ?
call ? ? ? ?<lambda_0e153cdea67ea404383c23c1022dd325>::operator()?
第一行,rcx寄存器清0,即this指針置0;第二行,調用operator()。
綜合上面的分析,得出Lambda轉換成函數指針的一種可能的實現方式,參考下面的代碼:
typedef int(*FUNCADD)(int, int);
// 實現兩個整數相加的函數對象
class add_function_object
{
public:
? ? // 函數調用運算符
? ? int operator()(int x, int y)const
? ? {
? ? ? ? return x + y;
? ? }
? ? // 函數指針類型轉換運算符
? ? operator FUNCADD()const
? ? {
? ? ? ? return add;
? ? }
private:
? ? static int add(int x, int y)
? ? {
? ? ? ? add_function_object* obj = nullptr;
? ? ? ? return obj->operator()(x, y);
? ? }
};
void example2()
{
? ? int x = 2, y = 3;
? ? add_function_object add;
? ? int z1 = add(x, y);
? ? auto fadd = add.operator FUNCADD();
? ? int z2 = fadd(x, y);
? ? cout << z1 << ", " << z2 << endl;
}
從C++17起,沒有捕獲任何變量的Lambda可以用作值類型模板實參,參考下面的代碼:
typedef int (*INTEGER_OPERATION)(int, int);
int do_integer_operation(INTEGER_OPERATION op, int x, int y)
{
? ? return op(x, y);
}
template <INTEGER_OPERATION op>
int integer_operation_t(int x, int y)
{
? ? return op(x, y);
}
void example3()
{
? ? auto add = [](int x, int y)
? ? {
? ? ? ? return x + y;
? ? };
? ? auto sub = [](int x, int y)
? ? {
? ? ? ? return x - y;
? ? };
? ? int z1 = integer_operation_t<add>(2, 3);
? ? int z2 = do_integer_operation(integer_operation_t<sub>, 2, 3);
? ? cout << "z1 : " << z1 << ", z2 : " << z2 << endl;
}
原文鏈接:https://blog.csdn.net/u011680671/article/details/99660594
相關推薦
- 2022-08-21 C#?Any()和AII()方法的區別_C#教程
- 2022-06-17 C語言深入講解函數參數的使用_C 語言
- 2022-11-16 Docker如何安全地停止和刪除容器_docker
- 2022-07-06 C#中LINQ?to?DataSet操作及DataTable與LINQ相互轉換_C#教程
- 2022-06-16 Kotlin操作符重載實例詳解_Android
- 2022-10-28 React?createElement方法使用原理分析介紹_React
- 2022-06-27 服務器端如何開啟GZIP壓縮功能(Apache、IIS、Nginx)_Linux
- 2022-06-25 pycharm中venv文件夾自定義處理方式圖解_相關技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支