如何迭代元组(使用C++ 11)?我尝试了以下方法:
for(int i=0; i<std::tuple_size<T...>::value; ++i)
std::get<i>(my_tuple).do_sth();
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
错误1:抱歉,未实现:无法将"Listener ..."扩展为固定长度的参数列表.
错误2:我不能出现在常量表达式中.
那么,我如何正确迭代元组的元素?
I have an overloaded function that I have to call with many different types. The simple approach is:
uint8_t a;
uint16_t b;
//....
double x;
doSomething(a);
doSomething(b);
//...
doSomething(x);
Run Code Online (Sandbox Code Playgroud)
expressing those calls succinctly can be done with a variadic template as explained at this Q&A. The code will look somewhat like this:
auto doSomethingForAllTypes = [](auto&&... args) {
(doSomething(args), ...);
};
uint8_t a;
uint16_t b;
//....
double x;
doSomethingForAllTypes(a, b, ... ,x);
Run Code Online (Sandbox Code Playgroud)
But I'll have to do this at …