相关疑难解决方法(0)

你如何迭代std :: tuple的元素?

如何迭代元组(使用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:我不能出现在常量表达式中.

那么,我如何正确迭代元组的元素?

c++ iteration template-meta-programming c++11 stdtuple

92
推荐指数
12
解决办法
6万
查看次数

C++ call function with many different types

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 …

c++ containers templates variadic

11
推荐指数
3
解决办法
994
查看次数