eATM

c++通过std::tuple包装参数调用函数

c++17方案

				
					auto f = [](int a, double b, std::string c) { std::cout<<a<<" "<<b<<" "<<c<< std::endl; };
auto params = std::make_tuple(1,2.0,"Hello");
std::apply(f, params);
				
			

c++14方案

				
					#include <vector>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <memory>
#include <cmath>
#include <algorithm>
#include <type_traits>
#include <iostream>
#include <functional>
#include <complex>


void f(int a, double b, void* c)
{
      std::cout << a << ":" << b << ":" << c << std::endl;
}

template<typename Function, typename Tuple, size_t ... I>
auto call(Function f, Tuple t, std::index_sequence<I ...>)
{
     return f(std::get<I>(t) ...);
}

template<typename Function, typename Tuple>
auto call(Function f, Tuple t)
{
    static constexpr auto size = std::tuple_size<Tuple>::value;
    return call(f, t, std::make_index_sequence<size>{});
}

int main()
{
    std::tuple<int, double, int*> t;
    call(f, t);    
}


				
			

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注