c++ - Thread C++ 成员函数模板可变参数模板

标签 c++ multithreading templates c++11

我尝试使用线程调用对象的成员函数。

如果函数没有可变参数模板 (Args ... args),没问题,它可以工作:

考虑这两个类:

基因引擎

template <class T>
class GeneticEngine
{
    template <typename ... Args>
    T* run_while(bool (*f)(const T&),const int size_enf,Args& ... args)
    {
         std::thread(&GeneticThread<T>::func,islands[0],f,size_enf);
         /* Some code */
         return /* ... */
    }
    private:
        GeneticThread<T>** islands;


}

遗传线程

template <class T>
class GeneticThread
{
    void func(bool (*f)(const T&),const int size_enf)
    {
        /* Some code */
    };
}

有了这个就OK了。


现在,我向相同的函数添加一个可变参数模板:

基因引擎

template <class T>
class GeneticEngine
{
    template <typename ... Args>
    T* run_while(bool (*f)(const T&,Args& ...),const int size_enf,Args& ... args)
    {
       std::thread(&GeneticThread<T>::func,islands[0],f,size_enf,args ...);
       /* Other code ... */
    }
    private:
        GeneticThread<T>** islands;
}

遗传线程

template <class T>
class GeneticThread
{
    template <typename ... Args>
    void func(bool (*f)(const T&,Args& ...), const int size_enf, Args& ... args)
    {
       /* Code ... */
    };
 }

使用此代码,GCC 无法编译:(对于此错误消息,我们深表歉意)

g++ main.cpp GeneticEngine.hpp GeneticThread.hpp Individu.hpp GeneticThread.o -g -std=c++0x    -o main.exe
In file included from main.cpp:2:0:
GeneticEngine.hpp: In instantiation of ‘T* GeneticEngine<T>::run_while(bool (*)(const T&, Args& ...), int, Args& ...) [with Args = {}; T = Individu]’:
main.cpp:24:78:   required from here
GeneticEngine.hpp:20:13: erreur: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, GeneticThread<Individu>*&, bool (*&)(const Individu&), const int&)’
GeneticEngine.hpp:20:13: note: candidates are:
In file included from GeneticThread.hpp:14:0,
             from GeneticEngine.hpp:4,
             from main.cpp:2:
/usr/include/c++/4.7/thread:131:7: note: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
/usr/include/c++/4.7/thread:131:7: note:   template argument deduction/substitution failed:
In file included from main.cpp:2:0:
GeneticEngine.hpp:20:13: note:   couldn't deduce template parameter ‘_Callable’
In file included from GeneticThread.hpp:14:0,
             from GeneticEngine.hpp:4,
             from main.cpp:2:
/usr/include/c++/4.7/thread:126:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.7/thread:126:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/4.7/thread:122:5: note: std::thread::thread()
/usr/include/c++/4.7/thread:122:5: note:   candidate expects 0 arguments, 4 provided

我真的不明白为什么这个差异会导致这个错误。我无法修复它。


在 main.cpp 中都是这样的:

/* Individu is another class */

int pop_size = 1000;
int pop_child = pop_size*0.75;

GeneticEngine<Individu> engine(/*args to constructor*/);

bool (*stop)(const Individu&) = [](const Individu& best){
    return false;
};
Individu* best = engine.run_while(stop,pop_child);

我使用:«gcc 版本 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)»


我试过:

std::thread(&GeneticThread<T>::func<Args...>, islands[0], f, size_enf, args ...);

现在我有另一个错误:

GeneticEngine.hpp: In member function ‘T* GeneticEngine<T>::run_while(bool (*)(const T&, Args& ...), int, Args& ...)’:
GeneticEngine.hpp:20:53: erreur: expansion pattern ‘((& GeneticThread<T>::func) < <expression error>)’ contains no argument packs
GeneticEngine.hpp:20:24: erreur: expected primary-expression before ‘(’ token
GeneticEngine.hpp:20:53: erreur: expected primary-expression before ‘...’ token



template <typename ... Args>
T* run_while(bool (*f)(const T&,Args& ...), const int size_enf, Args& ... args)
{

   void (GeneticThread<T>::*ptm)(bool (*)(T const&, Args&...), int, Args&...) = &GeneticThread<T>::func;
   std::thread(ptm, islands[0], f, size_enf, args ...);
}

最佳答案

尝试:

std::thread(&GeneticThread<T>::func<Args...>, islands...

func 现在是一个模板函数。您必须指定其模板参数才能获取其地址。

关于c++ - Thread C++ 成员函数模板可变参数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408351/

相关文章:

c++ - 如何理解 "Pass By Reference Within a Function"w3schools示例?

c++ - 范围守卫/范围退出惯用语会标准化吗?

c++ - 错误 : expected constructor, 析构函数,或 ‘(’ token 之前的类型转换。即使我有一个构造函数?

c++ - 懒惰的enable_if在工作时 sleep ?

java - 如果操作系统挂起,正在 sleep 的线程会发生什么?

java - 如何从线程取回数组值

c++ - 模板类和重载 '=='

ruby - 如何并行启动 Ruby 命令

c++ - 使用策略模板类工厂创建策略模板类?

string - 如何在 Kotlin 字符串模板中嵌入 for 循环