boost ::线程和模板函数

Rar*_*rge 3 multithreading templates boost boost-thread

我试图在一个单独的线程上运行模板函数,但IntelliSense(VC++ 2010 Express)一直给我错误:"错误:没有构造函数的实例"boost :: thread :: thread"匹配参数列表"如果我尝试编译我得到这个错误:"错误C2661:'boost :: thread :: thread':没有重载函数需要5个参数"

错误只发生在我添加模板后所以我确定它与它们有关但我不知道是什么.

我传递给boost :: thread的两个参数是定义为的模板函数:

template<class F> 
void perform_test(int* current, int num_tests, F func, std::vector<std::pair<int, int>>* results);
Run Code Online (Sandbox Code Playgroud)

和:

namespace Sort
{

template<class RandomAccessIterator>
void quick(RandomAccessIterator begin, RandomAccessIterator end);

} //namespace Sort
Run Code Online (Sandbox Code Playgroud)

我试着像这样调用boost :: thread:

std::vector<std::pair<int, int>> quick_results;
int current = 0, num_tests = 30;
boost::thread test_thread(perform_test, &current, num_tests, Sort::quick, &quick_results);
Run Code Online (Sandbox Code Playgroud)

hka*_*ser 5

模板函数不是"真实"函数.编译器首先需要实例化它.但是对于编译器来说,它需要知道您希望实例化该函数的模板参数的类型.编译器无法从代码中推导出这些参数.

但是,如果您将代码重写为:

typedef std::vector<std::pair<int, int>> container;
typedef container::iterator iterator;

boost::thread test_thread(
    &perform_test<Sort::quick<iterator>>, 
    &current, 
    num_tests, 
    &Sort::quick<iterator>, 
    &quick_results); 
Run Code Online (Sandbox Code Playgroud)

它应该工作.