使用Boost传递类函数时出错
Invalid Template Error passing class function using Boost
尝试使用模板,其中参数为
template<class T, boost::function<long (T*)> &f>
static long myFunc(const vector<boost::shared_ptr<T>> &inputVector)
{ // do stuff}
这是我打的电话
long i = myFunc<MyClass, boost::bind(&MyClass::myClassFunc, _1)>(myInputVector);
,其中函数的签名是
long myClassFunc() const { return m_value; }
得到以下编译错误:
错误C2975: 'f': 'myFunc'的模板参数无效,期望的编译时常量表达式
我需要什么来编译这个?
将参数绑定到函数是一个运行时操作。当您将一个值作为模板参数传递时,该值必须在编译时已知。将boost::函数作为参数传递。
template<class T>
static long myFunc(const vector<boost::shared_ptr<T>> &inputVector, boost::function<long (T*)> &f)
{ // do stuff
}
这样说:
long i = myFunc<MyClass)>(myInputVector, boost::bind(&MyClass::myClassFunc, _1));
相关文章:
- 如何通过派生类函数更改基类中的向量
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 在类函数中初始化外部作用域变量
- c++ 在非类函数中使用类变量
- SDL_PollEvent() 无法捕获类函数内部SDL_QUIT?
- 从类成员函数到类 C 函数指针的转换
- 如何在模板类函数中分配结构值?
- 有没有办法将重载的类函数绑定到函数对象?
- 启动类函数作为失去引用的线程
- C++调用使用重写函数的父类函数
- 将值传递到另一个类函数在打印时为零
- 计算对类函数的所有调用次数
- 由于签名差异,调用了错误的子类函数
- 使用宏调用类函数
- 虚拟基类函数中派生类的大小
- 线程调用类函数时出错
- 从基类指针访问派生类函数时出错
- 使用类函数创建线程时出错
- 将类对象从main传递到类函数时出错
- 使用Boost传递类函数时出错