기본적으로 원하는 것은 Callable (함수 유형/lambda/Functor)을 가져 와서 람다를 반환하는 함수 템플릿을 만드는 것입니다. 당신은 변경할 수 있습니다 - 더 - 유사한 인수 목록을 -taking 원래의 반환 형식C++ 템플릿에서 호출 가능 유형 (인수 목록 및 반환 값)을 추론하는 방법
#include <iostream>
int func(int a,float b) {
return a+b;
}
struct callable {
int operator() (int a, float b) {
return a+b;
}
};
template <typename RV, typename... Args>
auto getLambdaFromCallable(RV(&func)(Args...)) {
auto l = [&](Args... args) -> RV {
return func(args...);
};
return l;
}
int main() {
auto f = getLambdaFromCallable(func);
std::cout << f(1,2.f);
std::cout << " " << typeid(f).name();
auto f2 = getLambdaFromCallable(callable{}); // doesn't work
callable{}(1,2); // works
auto lambdaTest = [](int a, float b) -> int {
return a+b;
};
auto f3 = getLambdaFromCallable(lambdaTest);
}
람다 (참조를 처리)의 반환 유형에'-> decltype (자동)'. – Jarod42
심지어'-> decltype (func (std :: forward (args) ...))'이므로 SFINAE 친화적입니다. –
Jarod42
@ Jarod42 감사합니다. – Drax