【发布时间】:2017-06-08 21:06:06
【问题描述】:
我正在尝试了解绑定到模板类型方法的语法。 This 似乎与我的问题相似,但似乎没有给出绑定和调用模板类型方法的示例。
这是我的代码示例:
#include <functional>
#include <iostream>
using namespace std;
void DidSomething(int x)
{
cout << "Did something x = " << x << endl;
}
template <typename T>
class Outer
{
public:
void StartSomething()
{
Inner inner;
// below lines cause
// error C2893 Failed to specialize function template
//'unknown-type std::invoke(_Callable &&,_Types &&...)'
auto fnGlobal = std::bind(&::DidSomething);
inner.DoOneThing(fnGlobal);
auto fnMethod = std::bind(&Outer<T>::DidSomething, this);
inner.DoOneThing(fnMethod);
}
void DidSomething(int x)
{
cout << "Did something x = " << x << endl;
}
// example typedef, the actual callback has a lot of args (5 args)
typedef std::function<void(int)> DidSomethingCallback;
private:
class Inner
{
public:
void DoOneThing(DidSomethingCallback fnDidSomething)
{
fnDidSomething(3);
}
};
T t;
};
int main()
{
Outer<bool> outer;
outer.StartSomething();
return 0;
}
【问题讨论】: