Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 螺纹C和x2B+;成员函数模板变量模板_C++_Multithreading_Templates_C++11 - Fatal编程技术网

C++ 螺纹C和x2B+;成员函数模板变量模板

C++ 螺纹C和x2B+;成员函数模板变量模板,c++,multithreading,templates,c++11,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::t

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

如果函数没有可变模板(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 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;
}
模板
类遗传引擎
{
模板
T*运行时(bool(*f)(常量T&),常量int size_enf,Args&…Args)
{
std::thread(&GeneticThread::func,孤岛[0],f,大小_enf);
/*一些代码*/
返回/**/
}
私人:
日内瓦群岛;
}
遗传线程

template <class T>
class GeneticThread
{
    void func(bool (*f)(const T&),const int size_enf)
    {
        /* Some code */
    };
}
template <class T>
class GeneticThread
{
    template <typename ... Args>
    void func(bool (*f)(const T&,Args& ...), const int size_enf, Args& ... args)
    {
       /* Code ... */
    };
 }
模板
类基因线程
{
无效函数(布尔(*f)(常数T&),常数int size\u enf)
{
/*一些代码*/
};
}
有了这个,没关系


现在,我将变量模板添加到相同的函数中:

遗传引擎

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 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;
}
模板
类遗传引擎
{
模板
T*运行时(bool(*f)(常量T&,Args&…),常量int size,Args&…Args)
{
std::thread(&GeneticThread::func,孤岛[0],f,size_enf,args…);
/*其他代码*/
}
私人:
日内瓦群岛;
}
遗传线程

template <class T>
class GeneticThread
{
    void func(bool (*f)(const T&),const int size_enf)
    {
        /* Some code */
    };
}
template <class T>
class GeneticThread
{
    template <typename ... Args>
    void func(bool (*f)(const T&,Args& ...), const int size_enf, Args& ... args)
    {
       /* Code ... */
    };
 }
模板
类基因线程
{
模板
void func(bool(*f)(常数T&,参数&…),常数int size_enf,参数&…参数)
{
/*代码*/
};
}
有了这段代码,GCC就不会编译:(很抱歉出现了这个错误消息)

g++main.cpp GeneticEngine.hpp GeneticThread.hpp Individu.hpp GeneticThread.o-g-std=c++0x-o main.exe
在main.cpp中包含的文件中:2:0:
GeneticEngine.hpp:在“T*GeneticEngine::run_while(bool(*)(常量T&,Args&…),int,Args&…)[带Args={};T=Individu]”的实例化中:
main.cpp:24:78:从这里开始需要
GeneticEngine.hpp:20:13:erreur:调用“std::thread::thread(,GeneticThread*&,bool(*&)(const Individu&),const int&)”时没有匹配的函数
GeneticEngine.hpp:20:13:注:候选人为:
在GeneticThread.hpp:14:0中包含的文件中,
来自GeneticEngine。hpp:4,
来自main.cpp:2:
/usr/include/c++/4.7/thread:131:7:注意:模板std::thread::thread(_Callable&&,_Args&&&…)
/usr/include/c++/4.7/thread:131:7:注意:模板参数推断/替换失败:
在main.cpp中包含的文件中:2:0:
GeneticEngine.hpp:20:13:注意:无法推断模板参数“\u Callable”
在GeneticThread.hpp:14:0中包含的文件中,
来自GeneticEngine。hpp:4,
来自main.cpp:2:
/usr/include/c++/4.7/thread:126:5:注意:std::thread::thread(std::thread&&)
/usr/include/c++/4.7/thread:126:5:注意:候选者需要1个参数,提供4个
/usr/include/c++/4.7/thread:122:5:注意:std::thread::thread()
/usr/include/c++/4.7/thread:122:5:注意:候选者需要0个参数,提供4个
我真的不明白为什么这种差异会导致这种错误。我无法修复它


在这两种情况下,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);
/*Individu是另一个类*/
int pop_size=1000;
int pop_child=pop_size*0.75;
GeneticEngine引擎(/*参数到构造函数*/);
布尔(*停止)(常数个体和)=[](常数个体和最佳){
返回false;
};
Individu*best=引擎。运行时(停止,弹出子对象);

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


我试过:

std::thread(&GeneticThread<T>::func<Args...>, islands[0], f, size_enf, args ...);
std::thread(&GeneticThread::func,孤岛[0],f,size\u 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 ...);
}
geneticeengine.hpp:在成员函数“T*geneticeengine::run_while(bool(*)(常量T&,Args&…),int,Args&…)”中:
GeneticEngine.hpp:20:53:erreur:expansion模式“(&GeneticThread::func)<)”不包含参数包
GeneticEngine.hpp:20:24:erreur:应在“(”标记之前使用主表达式
GeneticEngine.hpp:20:53:erreur:应在“…”标记之前使用主表达式
模板
T*运行时(bool(*f)(常量T&,Args&…),常量int size,Args&…Args)
{
void(geneticshread::*ptm)(bool(*)(T常量和,Args和…),int,Args和…)=&geneticshread::func;
标准:螺纹(ptm,岛[0],f,尺寸,参数;
}
试试:

std::thread(&GeneticThread::func,孤岛。。。

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

尝试
&GenericThread::func
。GCC的模式扩展似乎有问题。您可以尝试
auto ptm=&GenericThread::func;
void(GenericThread::*ptm)(bool(*)(T常数和,参数和…),int,参数和…)=&GenericThread::func;
,然后分别将
ptm
传递给
std::thread
构造函数?非常感谢@Luc Danton。尽可能简洁地说,函数模板不是函数,也不存在获取函数模板地址的情况。相反,在这种情况下,函数模板的名称表现为如果它是一组重载函数的名称。请参阅何时获取重载函数的地址。(GCC无法获取函数模板专用化的地址应该是一个错误。)谢谢,我现在理解得更好了。