1
我有,我不能绑定此模板成员函数一个奇怪的问题, 所有这些代码编译:http://ideone.com/wl5hS8的std ::绑定模板成员函数
这是一个简单的代码:我有一个ExecutionList
应持有可调用的函数在std::vector
。我现在可以通过调用ExecutionList::addFunctor
来添加功能。但在那里,我不能绑定到template<typename T> void Functor::calculate(T * t)
。我问我为什么,是什么在这里失踪,编译器不能莫名其妙地推断我已经写在
m_calculationList.push_back(std::bind(&T::calculate<Type>, extForce, std::placeholders::_1));
* 错误:*
error: expected primary-expression before ‘>’ token
m_calculationList.push_back(std::bind(T::calculate<Type>, extForce, std::placeholders::_1));
^
验证码:
#include <functional>
#include <iostream>
#include <vector>
struct MyType{
static int a;
int m_a;
MyType(){a++; m_a = a;}
void print(){
std::cout << "MyType: " << a << std::endl;
}
};
int MyType::a=0;
struct Functor{
static int a;
int m_a;
Functor(){a++; m_a = a;}
// Binding this function does not work
template<typename T>
void calculate(T * t){}
// Binding this function works!!!
void calculate2(MyType * t){
std::cout << " Functor: " << m_a <<std::endl;
t->print();
}
};
int Functor::a=0;
// Binding this function works!!!
template<typename T>
void foo(T * t){}
class ExecutionList{
public:
typedef MyType Type;
template<typename T>
void addFunctor(T * extForce){
//m_calculationList.push_back(std::bind(&T::calculate<Type>, extForce, std::placeholders::_1)); /// THIS DOES NOT WORK
m_calculationList.push_back(std::bind(&T::calculate2, extForce, std::placeholders::_1));
m_calculationList.push_back(std::bind(&foo<Type>, std::placeholders::_1));
}
void calculate(Type * t){
for(auto it = m_calculationList.begin(); it != m_calculationList.end();it++){
(*it)(t); // Apply calculation function!
}
}
private:
std::vector< std::function<void (Type *)> > m_calculationList;
};
int main(){
MyType b;
ExecutionList list;
list.addFunctor(new Functor());
list.addFunctor(new Functor());
list.calculate(&b);
}
那意味着我需要一些''typename''字样?这是否合理,因为它不是一个类型,而是一个函数指针 – Gabriel
还有另一个消歧器:'模板'。 'std :: bind(&T :: template calculate,...)'应该工作。此外,*总是*包括你得到的错误信息。 “不工作”不起作用。 –
Xeo
ahhh damm it,我认为它是这些愚蠢的东西之一,但从来没有使用过某种类型的模板,只是有时候如''a.template foo'''如果'foo'不可扣除 –
Gabriel