我想將不同類型的函子傳遞給一個方法。因此我認爲應該使用std :: function。但是,由於此方法還應存儲對函數對象的引用,所以我想要傳遞shared_ptr(用於生命週期管理)。下面的代碼適用於B類(b.run(...)),但無法編譯爲A類(a.run(...)break)。 當傳遞一個指針而不是函數對象本身時,這個轉換問題的原因是什麼,我該如何繞過它?將函數對象指針傳遞給接受指向std :: function的函數的指針
#include <functional>
#include <memory>
class MyFunctor
{
public:
void operator()(const float &f)
{}
};
template<class FunSig>
class A
{
public:
void run(std::shared_ptr<std::function<FunSig> > f_ptr)
{
// store f_ptr in a vector
}
};
template<class FunSig>
class B
{
public:
void run(std::function<FunSig> f)
{}
};
int main()
{
MyFunctor mf1;
std::shared_ptr<MyFunctor> mf2_ptr(new MyFunctor);
A<void (const float &)> a;
B<void (const float &)> b;
a.run(mf2_ptr); // this breaks!
b.run(mf1); // this works
}
編譯器錯誤:
error: no matching function for call to ‘A<void(const float&)>::run(std::shared_ptr<MyFunctor>&)’
note: candidate is:
note: void A<FunSig>::run(std::shared_ptr<std::function<FunSig> >) [with FunSig = void(const float&)]
note: no known conversion for argument 1 from ‘std::shared_ptr<MyFunctor>’ to ‘std::shared_ptr<std::function<void(const float&)> >
現在我發現,a.run(...)編譯如果MyFunctor從性病::功能繼承:
class MyFunctor : public std::function<void (const float &)>
爲什麼這在工作,在忙?如果在函子中沒有必要更改代碼,我會更好。
這裏的問題是一個類型轉換。 MyFunctor *是指向MyFunctor結構的指針,std :: function *是指向std :: function對象的指針:當MyFunctor從std :: function繼承時,代碼會編譯,因爲它現在是一個子類std :: function,因此可以隱式轉換。 –
IdeaHat
2013-04-09 16:23:02