Created
          March 14, 2019 19:05 
        
      - 
      
 - 
        
Save Pibben/720bf548bd2921028f97060dbd556c4e to your computer and use it in GitHub Desktop.  
  
    
      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 <functional> | |
| #include <string> | |
| #include <tuple> | |
| #include <array> | |
| template <class T> | |
| T parseHelper(const std::string& str) { return T(5); } | |
| template<class... Args, size_t... I> | |
| void parse2(std::tuple<Args...>& tuple, const std::array<std::string, sizeof...(Args)>& data, std::index_sequence<I...>) { | |
| ((std::get<I>(tuple) = parseHelper<int>(std::get<I>(data))), ...); | |
| } | |
| template <class... Args> | |
| void parse(std::tuple<Args...>& tuple, const std::array<std::string, sizeof...(Args)>& data) { | |
| parse2(tuple, data, std::make_index_sequence<sizeof...(Args)>{}); | |
| } | |
| int test() { | |
| std::array<std::string, 1> t{"Hello"}; | |
| std::tuple<int> r; | |
| parse(r, t); | |
| return std::get<0>(r); | |
| } | |
| // https://stackoverflow.com/questions/28509273/get-types-of-c-function-parameters | |
| // https://stackoverflow.com/questions/11893141/inferring-the-call-signature-of-a-lambda-or-arbitrary-callable-for-make-functio | |
| // https://stackoverflow.com/questions/21657627/what-is-the-type-signature-of-a-c11-1y-lambda-function | |
| template <class... T> | |
| struct TypeList { | |
| static const size_t size = sizeof...(T); | |
| }; | |
| template<class Func, class... Args, size_t... I> | |
| void parseFuncHelper(Func func, const std::array<std::string, sizeof...(Args)>& data, TypeList<Args...>, std::index_sequence<I...>) { | |
| (func(parseHelper<int>(std::get<I>(data))), ...); | |
| } | |
| // For generic types that are functors, delegate to its 'operator()' | |
| template <typename T> | |
| struct function_traits | |
| : public function_traits<decltype(&T::operator())> | |
| {}; | |
| // for pointers to member function | |
| template <typename ClassType, typename ReturnType, typename... Args> | |
| struct function_traits<ReturnType(ClassType::*)(Args...) const> { | |
| using types = TypeList<Args...>; | |
| }; | |
| template <typename ReturnType, typename... Args> | |
| struct function_traits<ReturnType (*)(Args...)> { | |
| using types = TypeList<Args...>; | |
| }; | |
| template<size_t N, typename T> | |
| void parseFunc(T func, const std::array<std::string, N>& data) { | |
| using args = typename function_traits<T>::types; | |
| parseFuncHelper(func, data, args{}, std::make_index_sequence<args::size>{}); | |
| } | |
| static int a = 7; | |
| void func(int b) { a = b;} | |
| int test2() { | |
| std::array<std::string, 1> t{"Hello"}; | |
| int c; | |
| auto lambda = [&c](int b) -> void { c = b; }; | |
| //parseFunc(std::function<void(int)>(func), t); | |
| //parseFunc(func, t); | |
| parseFunc(lambda, t); | |
| return c; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment