1

は、私は次のコードを持っている:メンバ関数&constメンバ関数ポインタ控除

template <class Ret> 
class Foo 
{ 
public: 
    template <class T> 
    void foo(T&, const std::function<Ret()>&) 
    { 
     std::cout << "std::function<Ret()>\n"; 
    } 
    template <class T> 
    void foo(T&, Ret(T::*)() const) 
    { 
     std::cout << "Ret(T::*)() const\n"; 
    } 
    template <class T> 
    void foo(T&, Ret(T::*)()) 
    { 
     std::cout << " Ret(T::*)()\n"; 
    } 
}; 

class A 
{ 
public: 
    void foo1() const 
    { 
    } 
    void foo() 
    { 
    } 
}; 

int main() 
{ 

    A a; 
    Foo<void> f; 
    f.foo(a, &A::foo); 
    f.foo(a, &A::foo1); 
    f.foo(a, std::bind(&A::foo, a)); 
} 

をそれはうまく動作しますが、私はのconstと非constメンバ関数ポインタで、2つの異なる機能を持っている必要はありません。そこで質問です: 無効のfoo(T &、constのはstd ::機能&) & ボイドのfoo(T &、Retの(T :: *)()constは) 1の機能をマージする方法があります?注意:std :: function overloadはマージ後に解決に参加する必要があります。私はメンバ関数ポインタだけを取るいくつかの関数が必要です。そして他のすべてのものはstd :: functionバージョンへの道を開くでしょう。

ありがとうございます!

答えて

1

あなたは1つの暗黙の質問と1つの明示的な質問をしているようです。暗黙の質問「constとnonconstのバージョンをマージする方法」への答えは次のとおりです。

template<typename T, typename U, typename enable_if<is_fuction<T>::value, int>::type = 0> 
void takesboth(T U::*); 
+0

手がかりをありがとう!私はenable_ifについても考えていない。 – ixSci

関連する問題