1

私は次のコードを持っています:

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とnon-constのメンバー関数ポインターに2つの異なる関数を使用したくありません。したがって、問題は次のとおりです 。1つの関数にマージする方法はvoid foo(T&, const std::function<Ret()>&)ありますか?マージ後の解決にも関与する必要void foo(T&, Ret(T::*)() const)がある過負荷があることに注意してください。std::functionメンバー関数ポインターのみを受け取る関数が必要です。そして、他のすべてはstd::functionバージョンへの道を作ります。

4

1 に答える 1

1

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

template<typename T, typename U, typename enable_if<is_fuction<T>::value, int>::type = 0> 
void takesboth(T U::*);
于 2012-04-18T08:11:51.730 に答える