2013-05-06 105 views
5

我嘗試使用線程調用對象的成員函數。線程C++成員函數模板variadic模板

如果函數不具有可變參數模板(參數...參數),沒有問題,它的工作原理:

考慮兩類:

GeneticEngine

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; 


} 

GeneticThread

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

這樣就可以了。


現在,我添加了一個可變參數模板相同的功能:

GeneticEngine

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; 
} 

GeneticThread

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 ...); 
} 
+0

嘗試'&GenericThread :: func '。 – 2013-05-06 22:40:35

+1

看起來GCC在模式擴展方面遇到了麻煩。你可以嘗試'auto ptm =&GenericThread :: func ;'或'void(GenericThread :: * ptm)(bool(*)(T const&,Args&...),int,Args&...)= &GenericThread :: func;'分別將'ptm'傳遞給'std :: thread'構造函數? – 2013-05-06 22:52:10

+0

謝謝@Luc Danton。 – Krozark 2013-05-06 23:02:40

回答

2

嘗試:

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

func現在是一個模板函數。您必須指定其模板參數才能取得其地址。

+0

我嘗試了一下,但出現了另一個錯誤(請參閱編輯)。 – Krozark 2013-05-06 22:48:42