c++ 可变参数的打包到tuple,使用tuple传递参数
直接上代码
void Add(int a, double b, short c, const char * f) { std::cout << f << a << ", " << b << ", " << c << "; "; } void *p = nullptr; template <typename ... Args> void CallLater(Args... args) { auto c = std::make_tuple(args...); auto ptuple = new decltype(c); *ptuple = c; p = (void *)ptuple; } void doCall(){ using v = std::tuple<int , double , short , const char * >; v * ptuple = static_cast<v *>(p); std::apply(Add, *ptuple); delete ptuple; p = nullptr; } void main() { CallLater(1, 3.5, (short)3, "this"); doCall(); }