_Callable __f
が(特定の非静的メンバー関数へのポインタ)memberfuncA
であるかどうかをテストします。
私は__f == &MyClass::memberfuncA
でそれをテストできますが、以下の例のようにmemberfuncBを取得しないとコンパイルエラーになります。
私はstd :: is_convertibleを使ってキャストがコンパイルされるようにしようとしましたが、それは動作しませんでした。 typeid()と同じです。特定のメンバー関数で呼び出し可能ポイントをテストする場合
#include <type_traits>
#include <typeinfo>
class A {};
class B {};
class MyClass {
public:
MyClass() {
A a;
B b;
push_exec(&MyClass::memberfuncA, this, a);
push_exec(&MyClass::memberfuncB, this, b);
}
template<typename _Callable, typename... _Args>
void push_exec(_Callable&& __f, _Args&&... __args)
{
// Test if typeid is the same
if(typeid(__f) == typeid(&MyClass::memberfuncA))
// Test if types are castable
if(std::is_convertible<typeof __f, typeof &MyClass::memberfuncA>::value)
// Test if __f is actually pointing to a specific member function (compiler error)
if(__f == &MyClass::memberfuncA)
return;
}
void memberfuncA(A a) { }
void memberfuncB(B b) { }
};
int main() {
MyClass mc;
}
コンパイラの出力が
g++ --std=gnu++11 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/callable_test.d" -MT"src/callable_test.o" -o "src/callable_test.o" "../src/callable_test.cpp"
../src/callable_test.cpp: In instantiation of ‘void MyClass::push_exec(_Callable&&, _Args&& ...) [with _Callable = void (MyClass::*)(B); _Args = {MyClass* const, B&}]’:
../src/callable_test.cpp:13:49: required from here
../src/callable_test.cpp:24:24: error: comparison between distinct pointer types ‘void (MyClass::*)(B)’ and ‘void (MyClass::*)(A)’ lacks a cast [-fpermissive]
if(__f == &MyClass::memberfuncA)
^
../src/callable_test.cpp:24:24: error: cannot convert ‘__f’ from type ‘void (MyClass::*)(B)’ to type ‘void MyClass::*’
../src/callable_test.cpp:24:24: error: cannot convert ‘&MyClass::memberfuncA’ from type ‘void (MyClass::*)(A)’ to type ‘void MyClass::*’
make: *** [src/callable_test.o] Error 1
私は__f == &MyClass::memberfuncA
かどうかを確認することができますどのように任意のアイデアですか?それはコンパイルの問題ではなく、実行時エラーだから
これは問題ではありませんが、2つのアンダースコア( '__f')で始まる名前は、実装に予約されています。それらを使用しないでください。 –
アプローチの「部分テンプレートの特殊化」を参照してください。 – Peter