![](/img/trans.png)
[英]Iterating through parameters of a variadic function template using variadic lambda
[英]iterating over variadic template's type parameters
我有一個這樣的函數模板:
template <class ...A>
do_something()
{
// i'd like to do something to each A::var, where var has static storage
}
我不能使用Boost.MPL
。 你能展示如何在沒有遞歸的情況下做到這一點嗎?
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;
}
該程序打印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>>();
}
上面的答案有效——在這里,我將更多地探討如何將 lambda 用於復雜的用例。
Lambda 101: [ capture
]( params
){ code
}( args to call "in-place"
);
如果你想用可變參數模板擴展 lambda,當參數是非平凡類型時,它不會像上面提到的那樣工作:
error: cannot pass object of non-trivial type 'Foo' through variadic method; call will abort at runtime
error: cannot pass object of non-trivial type 'Foo' through variadic method; call will abort at runtime
。
要走的路是將代碼從 lambda 的args
到code
:
template <class ...A>
do_something() {
Foo foo;
[&foo](var...){
foo.DoSomething(var);
}(A::var...);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.