如何迭代元组(使用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:我不能出现在常量表达式中.
那么,我如何正确迭代元组的元素?
我有一个未知大小的元组(它是方法的模板参数)
是否可以获得它的一部分(我需要丢掉它的第一个元素)
例如,我有tuple<int,int,int>(7,12,42).我想要tuple<int,int>(12,42)在这里
我做了以下元组:
我想知道如何迭代它?有tupl_size(),但阅读文档,我没有得到如何利用它.我也搜索了SO,但问题似乎就在附近Boost::tuple.
auto some = make_tuple("I am good", 255, 2.1);
Run Code Online (Sandbox Code Playgroud) 我有多个类(Foo和Bar这里简单)
struct Bar {};
struct Foo {};
Run Code Online (Sandbox Code Playgroud)
和一个采用单个模板参数并根据该类型执行某些操作的函数:
template <typename T>
constexpr void doSomething() { cout << "Am I a Foo? " << is_same<T,Foo>::value << endl; }
Run Code Online (Sandbox Code Playgroud)
在我的代码中,为我提供了Foos和Bars 的模板参数包,并且应该doSomething()在它们的每个参数上调用函数(我不在乎函数的执行顺序)。
doStuff<Foo, Bar, Bar>(); // --> True / False / False
Run Code Online (Sandbox Code Playgroud)
到目前为止,我唯一能想到的解决方案是:
template <typename... Ts>
class Doer;
template <>
struct Doer <> {
static constexpr void doStuff() {}
};
template <typename Head, typename... Tail>
struct Doer <Head, Tail...> {
static constexpr void …Run Code Online (Sandbox Code Playgroud) c++ variadic-functions function-templates template-meta-programming variadic-templates