[英]Use std::function with lambda and variadic template
我在互聯網上找到了一些不錯的循環功能,但是無法將其變成我自己的。 並且代碼被取消注釋,因此沒有幫助。
原始代碼:
template <typename T> struct identity { typedef T type; };
template <typename ... Components>
void each(typename identity<std::function<void(Base base, Components&...)>>::type f) {
//do stuff
}
目標是從類中解壓縮一些數據並在其上應用函數,Foo有包含該對象的管理器,並且該對象內部有一些int成員。我希望它足夠清楚。
我想實現這樣的目標:
#include <functional>
#include <iostream>
class Foo {
template < typename ... T >
void for_each(std::function<void(T&...)> func) {
std::cout << "for_each" << std::endl;
}
};
int main() {
Foo test;
test.for_each<int>([](int& data) {
std::cout << "main" << std::endl;
});
return 0;
}
但我有這個錯誤:
Test.cpp: In function ‘int main()’:
Test.cpp:17:4: error: no matching function for call to ‘Foo::for_each(main()::<lambda(int&)>)’
});
^
Test.cpp:7:9: note: candidate: template<class ... T> void Foo::for_each(std::function<void(T& ...)>)
void for_each(std::function<void(T&...)> func) {
^~~~~~~~
Test.cpp:7:9: note: template argument deduction/substitution failed:
Test.cpp:17:4: note: ‘main()::<lambda(int&)>’ is not derived from ‘std::function<void(T& ...)>’
});
^
看起來這個代碼有一些錯誤,我希望不再是這種情況了。 如果你有一些建議讓這個代碼有效,那么你就會讓一個男人開心。
如果指定std::function
作為參數類型,則必須傳遞std::function
。 lambda不是std::function
,但它可以用來創建一個。
#include <functional>
#include <iostream>
class Foo {
public:
template < typename ... T >
void for_each(std::function<void(T&...)> func) {
std::cout << "for_each" << std::endl;
}
};
int main() {
Foo test;
test.for_each<int>(std::function<void(int&)>([](int& data) {
std::cout << "main" << std::endl;
}));
return 0;
}
這可能不是你想要的(目前,我只能猜測),這個編輯至少允許這個例子進行編譯。
基於
目標是從類中解壓縮一些數據並在其上應用函數,Foo有包含該對象的管理器,並且該對象內部有一些int成員
我想你正在尋找類似的東西:
#include <functional>
#include <iostream>
#include <vector>
class Foo {
public:
template < typename T >
void for_each(std::function<void(T&)> func) {
std::cout << "for_each" << std::endl;
for ( int i : data)
{
func(i);
}
}
std::vector<int> data;
};
int main() {
Foo test;
test.data = {1, 2, 3, 4, 5};
test.for_each<int>([](int& data) {
std::cout << "In main, data:" << data << std::endl;
});
return 0;
}
如果您計划僅使用非捕獲lambdas,則可以使用函數指針作為參數,並讓lambda衰減為這種類型。
它遵循一個最小的工作示例:
#include <iostream>
class Foo {
public:
template < typename ... T>
void for_each(void(*func)(T&...)) {
std::cout << "for_each" << std::endl;
}
};
int main() {
Foo test;
test.for_each(+[](int& data) {
std::cout << "main" << std::endl;
});
return 0;
}
如果您可以處理類模板,另一種可能的方法是:
#include <functional>
#include <iostream>
template < typename ... T >
class Foo {
public:
void for_each(std::function<void(T&...)> func) {
std::cout << "for_each" << std::endl;
}
};
int main() {
Foo<int> test;
test.for_each([](int& data) {
std::cout << "main" << std::endl;
});
return 0;
}
請注意,如果您實際上對類型T
不感興趣,可以使用泛型類型並使用它:
#include <iostream>
class Foo {
public:
template<typename F>
void for_each(F &&func) {
std::cout << "for_each" << std::endl;
}
};
int main() {
Foo test;
test.for_each([](int& data) {
std::cout << "main" << std::endl;
});
return 0;
}
它適用於捕獲和非捕獲lambda。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.