Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
lambdaでstd::function<>とTFunc<>対応させる C++Builder
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <type_traits>
#include <functional>
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Memo1->Clear();
///std::function<>を使って実行
unsigned int i3{func1<std::function<int(int, int)> >([](int i1, int i2)->int
{
return i1 + i2;
})};
///TFunc__3<>を使って実行
unsigned int i4{func1<_di_TFunc__3<int, int, int > >([](int i1, int i2)->int
{
return i1 + i2+ i2;//上とは若干違う内容
})};
OutputDebugString(Format(L"%d, %d", ARRAYOFCONST((i3, i4)) ).c_str() );
Memo1->Lines->Append(Format(L"std::function<int(int, int)> = %d", ARRAYOFCONST((i3))));
Memo1->Lines->Append(Format(L"TFunc__3<int, int, int > = %d", ARRAYOFCONST((i4))));
}
//---------------------------------------------------------------------------
template<typename T, bool >
int __fastcall func2(T fn1, int i1, int i2)
{
return fn1(i1, i2);
}
template<typename T, bool>
int __fastcall func2(_di_TFunc__3<int, int, int > fn1, int i1, int i2)
{
return (*fn1).Invoke(i1, i2);
}
template<typename T>
unsigned int __fastcall TForm1::func1(T fn1)//std::function<>とTFunc__3<>両方対応させる
{
int result_{0};
for (int i = 0; i < 10; i++)
{
result_ = func2<T, std::is_same<T, std::function<int(int, int)> >::value>(fn1, i, result_);
}
return static_cast<unsigned int>(result_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment