use*_*108 18 c++ templates variadic-templates c++11
我有一个这样的功能模板:
template <class ...A>
do_something()
{
// i'd like to do something to each A::var, where var has static storage
}
Run Code Online (Sandbox Code Playgroud)
我不能用Boost.MPL.你能告诉我如何在没有递归的情况下做到这一点吗?
jro*_*rok 17
Xeo说的是什么.要为包扩展创建上下文,我使用了一个不执行任何操作的函数的参数列表(dummy):
#include <iostream>
#include <initializer_list>
template<class...A>
void dummy(A&&...)
{
}
template <class ...A>
void do_something()
{
dummy( (A::var = 1)... ); // set each var to 1
// alternatively, we can use a lambda:
[](...){ }((A::var = 1)...);
// or std::initializer list, with guaranteed left-to-right
// order of evaluation and associated side effects
auto list = {(A::var = 1)...};
}
struct S1 { static int var; }; int S1::var = 0;
struct S2 { static int var; }; int S2::var = 0;
struct S3 { static int var; }; int S3::var = 0;
int main()
{
do_something<S1,S2,S3>();
std::cout << S1::var << S2::var << S3::var;
}
Run Code Online (Sandbox Code Playgroud)
这个程序打印111.
例如,假设您要显示每个A :: var.我看到了三种方法来实现这一点,如下面的代码所示.
关于选项2,请注意标准未指定处理元素的顺序.
#include <iostream>
#include <initializer_list>
template <int i>
struct Int {
static const int var = i;
};
template <typename T>
void do_something(std::initializer_list<T> list) {
for (auto i : list)
std::cout << i << std::endl;
}
template <class... A>
void expand(A&&...) {
}
template <class... A>
void do_something() {
// 1st option:
do_something({ A::var... });
// 2nd option:
expand((std::cout << A::var << std::endl)...);
// 3rd option:
{
int x[] = { (std::cout << A::var << std::endl, 0)... };
(void) x;
}
}
int main() {
do_something<Int<1>, Int<2>, Int<3>>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10120 次 |
| 最近记录: |