C++ Builder 參考手冊 ? System::Sysutils ? _di_TProc__4
頭文件:#include <System.SysUtils.hpp>
命名空間:System::Sysutils
類型定義:
template<typename T1, typename T2, typename T3, typename T4>
using _di_TProc__4 = System::DelphiInterface<TProc__4<T1, T2, T3, T4>>;
C++ 匿名函數(shù) / lambda 表達式接口:有 4 個參數(shù)、無返回值的匿名函數(shù) / lambda 表達式艰管,C++ Builder 采用這個接口讓 lambda 表達式與 Delphi 的匿名函數(shù)兼容。
參數(shù):這個 lambda 表達式或匿名函數(shù)有 4 個參數(shù)窒盐,參數(shù)類型為 _di_TProc__4 的模板參數(shù)類型;
返回值:無撤蟆。
- 調(diào)用 _di_TProc::Invoke(); 可以執(zhí)行 lambda 表達式殖熟。
- 如果函數(shù)的參數(shù)是這個類型的柬赐,可以采用繼承 System::TCppInterfacedObject<TProc__4<T1, T2, T3, T4>> 并且重載 Invoke 函數(shù)作為這個參數(shù)來代替 lambda 表達式绰姻,請參考本文后面及 TThread::CreateAnonymousThread 的例子枉侧。
- 使用 _di_TProc__4 這個類型需要 clang 編譯器
例1:寫一個函數(shù) MyFunc,參數(shù)為與 Delphi 匿名函數(shù)兼容的 lambda 表達式狂芋,lambda 表達式有一個 UnicodeString 和 兩個 int 類型的參數(shù)
在 Form1 上放一個 Memo 和一個 Button榨馁,在 Button 的 OnClick 事件里面執(zhí)行 MyFunc 函數(shù),第一個參數(shù)為 TStrings *帜矾,第二個參數(shù)為有一個 UnicodeString 和兩個 int 參數(shù)的 lambda 表達式翼虫;MyFunc 函數(shù)類似于 for_each 枚舉 pStrings 里面所有的行,每一行調(diào)用一次 lambda 表達式屡萤,傳入這一行文字的關(guān)鍵字珍剑、值、行號和總行數(shù):
void TForm1::MyFunc(TStrings *pStrings, _di_TProc__4<UnicodeString, UnicodeString, int, int> pLambda)
{
int iCount = pStrings->Count;
for(int iIndex=0; iIndex<iCount; iIndex++)
pLambda->Invoke(pStrings->Names[iIndex], pStrings->ValueFromIndex[iIndex], iIndex, iCount);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyFunc(Memo1->Lines, [this](UnicodeString sKey, UnicodeString sValue, int iIndex, int iCount){
ShowMessage(L"第" + IntToStr(iIndex+1) + L"/" + IntToStr(iCount) + L"個關(guān)鍵字:\"" + sKey + L"\"死陆,值:\"" + sValue + L"\"");
});
}
執(zhí)行結(jié)果:Memo1 里面有兩行文字:"Name=Hsuanlu" 和 "名字=玄坴"招拙,點擊按鈕,在 MyFunc 里面執(zhí)行了 2 次 lambda 表達式措译,分別為這兩行文字的關(guān)鍵字别凤、值、行號和總行數(shù)领虹,可以看到執(zhí)行結(jié)果為兩次彈出消息框规哪,分別為 '第1/2個關(guān)鍵字:"Name",值:"Hsuanlu"' 和 '第2/2個關(guān)鍵字:"名字"塌衰,值:"玄坴"'
例2:依然是前面例1的 MyFunc 函數(shù)诉稍,不用 lambda 表達式,采用繼承 System::TCppInterfacedObject<TProc__2<T1, T2>> 的方法調(diào)用這個函數(shù)
void TForm1::MyFunc(TStrings *pStrings, _di_TProc__4<UnicodeString, UnicodeString, int, int> pLambda)
{
int iCount = pStrings->Count;
for(int iIndex=0; iIndex<iCount; iIndex++)
pLambda->Invoke(pStrings->Names[iIndex], pStrings->ValueFromIndex[iIndex], iIndex, iCount);
}
class TMyProc : public TCppInterfacedObject<TProc__4<UnicodeString, UnicodeString, int, int>>
{
public:
void __fastcall Invoke(UnicodeString sKey, UnicodeString sValue, int iIndex, int iCount) // 這里是調(diào)用 lambda 執(zhí)行的內(nèi)容
{
ShowMessage(L"第" + IntToStr(iIndex+1) + L"/" + IntToStr(iCount) + L"個關(guān)鍵字:\"" + sKey + L"\"猾蒂,值:\"" + sValue + L"\"");
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyFunc(Memo1->Lines, new TMyProc()); // 用 TMyProc 對象代替 lambda均唉,自動銷毀
}
新版 C++ 的 lambda 表達式書寫方便是晨,由于參數(shù)是 _di_TProc__4 類型的肚菠,C++ Builder 的編譯結(jié)果會把前面 例1 的 lambda 表達式編譯成類似于 例2 的代碼,所以說他們的執(zhí)行效率應(yīng)該是一樣的罩缴。
相關(guān):
- System::TCppInterfacedObject
- System::DelphiInterface
- TThread::CreateAnonymousThread
- System::Sysutils::_di_TFunc__1
- System::Sysutils::_di_TFunc__2
- System::Sysutils::_di_TFunc__3
- System::Sysutils::_di_TFunc__4
- System::Sysutils::_di_TFunc__5
- System::Sysutils::_di_TPredicate__1
- System::Sysutils::_di_TProc
- System::Sysutils::_di_TProc__1
- System::Sysutils::_di_TProc__2
- System::Sysutils::_di_TProc__3
- System::Sysutils::_di_TProc__4
- System::Sysutils
C++ Builder 參考手冊 ? System::Sysutils ? _di_TProc__4