【问题标题】:Invalid Template Error passing class function using Boost使用Boost传递类函数的无效模板错误
【发布时间】:2011-04-28 21:10:32
【问题描述】:

尝试使用参数所在的模板

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”的模板参数无效,预期的编译时常量表达式

编译需要什么?

【问题讨论】:

  • 你到底想做什么?您既没有传递 MyClass 的任何实例,也没有 myClassFunc 将 T* 作为参数,无论模板参数有什么问题,这段代码都没有任何意义。

标签: c++ templates boost


【解决方案1】:

将参数绑定到函数是运行时操作。当您将值作为模板参数传递时,必须在编译时知道该值。将 boost::function 作为参数传递。

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

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2017-12-25
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多