Last active
April 2, 2021 13:17
-
-
Save Angeart/ae347c4bdd5ed8b02385c411a723f9d0 to your computer and use it in GitHub Desktop.
how to get lambda return type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <tuple> | |
| #include <type_traits> | |
| namespace stx | |
| { | |
| namespace lambda_detail | |
| { | |
| template<class Ret, class Cls, class IsMutable, class... Args> | |
| struct types | |
| { | |
| using is_mutable = IsMutable; | |
| enum { arity = sizeof...(Args) }; | |
| using return_type = Ret; | |
| template<size_t i> | |
| struct arg | |
| { | |
| typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; | |
| }; | |
| }; | |
| } | |
| template<class Ld> | |
| struct lambda_type | |
| : lambda_type<decltype(&Ld::operator())> | |
| {}; | |
| template<class Ret, class Cls, class... Args> | |
| struct lambda_type<Ret(Cls::*)(Args...)> | |
| : lambda_detail::types<Ret,Cls,std::true_type,Args...> | |
| {}; | |
| template<class Ret, class Cls, class... Args> | |
| struct lambda_type<Ret(Cls::*)(Args...) const> | |
| : lambda_detail::types<Ret,Cls,std::false_type,Args...> | |
| {}; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <typeinfo> | |
| #include "lambda_traits.hpp" | |
| int main(void){ | |
| std::cout << "[is mutable lambda]" << std::endl; | |
| { | |
| auto test = [](int a) mutable->long{return static_cast<long>(a); }; | |
| std::cout << "ret type : " << std::is_same<stx::lambda_type<decltype(test)>::return_type,long>::value << std::endl; | |
| std::cout << "arg size : " << stx::lambda_type<decltype(test)>::arity << std::endl; | |
| std::cout << "arg 0 type : " << std::is_same<stx::lambda_type<decltype(test)>::arg<0>::type,int>::value << std::endl; | |
| std::cout << "is mutable : " << std::is_same<stx::lambda_type<decltype(test)>::is_mutable,std::true_type>::value << std::endl; | |
| } | |
| std::cout << "[is normal lambda]" << std::endl; | |
| { | |
| auto test = [](int a, int b)->long{return static_cast<long>(a); }; | |
| std::cout << "ret type : " << std::is_same<stx::lambda_type<decltype(test)>::return_type,long>::value << std::endl; | |
| std::cout << "arg size : " << stx::lambda_type<decltype(test)>::arity << std::endl; | |
| std::cout << "arg 0 type : " << std::is_same<stx::lambda_type<decltype(test)>::arg<0>::type,int>::value << std::endl; | |
| std::cout << "is mutable : " << std::is_same<stx::lambda_type<decltype(test)>::is_mutable,std::true_type>::value << std::endl; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [is mutable lambda] | |
| ret type : 1 | |
| arg size : 1 | |
| arg 0 type : 1 | |
| is mutable : 1 | |
| [is normal lambda] | |
| ret type : 1 | |
| arg size : 2 | |
| arg 0 type : 1 | |
| is mutable : 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sadly, this one does not handle lambdas with auto arguments
[](auto &)