c++ 可變參數的打包到tuple,使用tuple傳遞參數


直接上代碼

 

void Add(int a, double b, short c, const char * f) {
	std::cout << f << a << ", " << b << ", " << c << ";\n";
}

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();
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM