[英]How to use template function parameter in std::bind?
i am trying to create a function that parallelize a for. 我正在尝试创建一个并行的功能。 I am using the Thread of the SFML and std::bind with template. 我正在使用SFML的线程和std :: bind与模板。
Here is what i tried. 这是我尝试过的。
#include <SFML/System.hpp>
#include <iostream>
#include <functional>
sf::Mutex mutex;
template <typename F>
void For_Multitread(F Function, std::vector<int>& A);
template <typename F>
void Execute_For(F Function, std::vector<int>& A, int Depart, int Fin);
void Compute(int& Number);
template <typename F>
void For_Multitread(F Function, std::vector<int>& A)
{
for (int Thread = 0; Thread < 2; Thread++)
{
sf::Thread thread(std::bind(&Execute_For, Function, A, Thread * 5, (Thread+1)*5));
thread.launch();
}
mutex.lock();
for (int i = 0; i < 10; ++i)
std::cout << A[i] << std::endl;
mutex.unlock();
}
template <typename F>
void Execute_For(F Function, std::vector<int>& A, int Depart, int Fin)
{
for (int i = Depart; i < Fin; ++i)
{
Function(A[i]);
}
}
void Compute(int& Number)
{
Number++;
}
int main()
{
std::vector<int> A;
for (int i =0; i < 10; i++)
A.push_back(i);
For_Multitread(&Compute, A);
return 0;
}
The error is 错误是
no matching function for call to 'bind(<unresolved overloaded function type>, void (*&)(int&), std::vector<int>&, int, int)
Did i miss something really important ? 我错过了真正重要的事情吗?
Execute_For
is a function template , therefore you need to either supply it with type template arguments: Execute_For
是一个函数模板 ,因此您需要为其提供类型模板参数:
std::bind(&Execute_For<F>, Function, A, Thread * 5, (Thread+1)*5)
// ~~~~~~~~~~~~~~^
or use a static_cast
: 或使用static_cast
:
std::bind(static_cast<void(*)(F,std::vector<int>&,int,int)>(&Execute_For), Function, A, Thread * 5, (Thread+1)*5)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.