からの呼び出しでmemFuncTaker
へのstd ::機能あなたは非静的メンバ関数ポインタにstd::function
をバインドすると、それはそれ解像度の最初の明示的なパラメータ作り、隠されたthis
パラメータを「明らかに」究極のファンクタ。あなたが好むのであればouter::aMember
ためのあなたのケースでは、あなたがstd::function<void(outer *, int)>
を使用したいと2パラメータファンクタもちろん
#include <functional>
#include <iostream>
template<class T>
void memFuncTaker(T *obj , std::function<void(T *, int)> func){
func(obj, 7);
}
class outer{
public:
void aMember(int a){
std::cout << a <<std::endl;
}
void anotherMember(double){
memFuncTaker(this, std::function<void(outer *, int)>{&outer::aMember});
}
};
int main() {
outer o;
o.anotherMember(0);
}
http://coliru.stacked-crooked.com/a/5e9d2486c4c45138
で終わる、あなたがによって(そのファンクタの最初の引数をバインドすることができます従ってstd::bind
又はラムダ)を使用して、このバージョンでmemFuncTaker
がもはやの主要目的の一つであることを起こるテンプレートを(なければならないこと
#include <functional>
#include <iostream>
using namespace std::placeholders;
void memFuncTaker(std::function<void(int)> func){
func(7);
}
class outer{
public:
void aMember(int a){
std::cout << a <<std::endl;
}
void anotherMember(double){
memFuncTaker(std::function<void(int)>(std::bind(&outer::aMember, this, _1)));
}
};
int main() {
outer o;
o.anotherMember(0);
}
注再び「隠していません」- コードを「テンプレート解除する」ためにタイプ消去技術を使用します)。
出典
2017-11-01 03:21:22
AnT
あなたのコードで 'std :: function'を使う試みはありません。 – AnT
あなたの質問タイトルは "std :: functionへのポインタをメンバー関数に変換する"と言いますが、 "std :: functionをメンバ関数へのポインタに変換する"のように聞こえます。 –