2013-05-06 8 views
5

私はスレッドを使ってオブジェクトのメンバ関数を呼び出そうとしています。スレッドC++のメンバ関数のテンプレートの可変的なテンプレート

機能は、可変長引数テンプレート(Argsの...引数)を持っていない場合は、何の問題は、それが動作:

を2つのクラスを考えてみません:

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 

なぜこの違いがこのエラーになるのかわかりません。私はそれを修正することはできません。私が使用

/* 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の/リナロ4.7.2-2ubuntu1)»

main.cppにはそのようなものです両方で



は、私が試した:

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 'を試してください。 –

+1

GCCがパターン拡張に問題があるようです。 auto ptm =&GenericThread :: func ;またはvoid(GenericThread :: * ptm)(bool(*)(T const&、Args&...)、int、Args&...)を試すことができますか? &GenericThread :: func; 'を別々に' ptm'を 'std :: thread'コンストラクタに渡しますか? –

+0

@ Luc Dantonありがとうございました。 – Krozark

答えて

2

試してみてください。

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

funcは現在、テンプレート関数です。そのアドレスを取るためには、テンプレートパラメータを指定する必要があります。

+0

試してみましたが、別のエラーが表示されます(編集を参照)。 – Krozark