2

mem_fun_refを使用して、オブジェクトのメンバー関数への参照を別の関数に送信しようとしていますが、error C2064: term does not evaluate to a function taking 0 arguments.

これを例に反映していませんが、mem_fun_ref_tを仮想関数に送信する必要があります。そのため、Flipを単純な関数オブジェクトを受け取る関数テンプレートにしただけではありません。

#include <iostream>
#include <string>
#include <functional>

class Coin
{
public:
    Coin() {}
    std::string Flip ()
    {
        srand(23);
        int side = rand() % 2 + 1;

        std::string result = "";
        if (side == 1)
            result = "heads.";
        else
            result = "tails.";
        return result;
    }
};



std::string Flip(std::mem_fun_ref_t<std::string, Coin> flip)
{
    return flip();
}



int main()
{
    std::cout << "Flipping a coin..." << std::endl;
    std::string output = Flip(std::mem_fun_ref<std::string, Coin>(&Coin::Flip));
    std::cout << "The coin came up " << output << std::endl;
    return 0;
}
4

1 に答える 1

3

静的メンバー関数メンバー関数ポインターについて読む必要があります。問題を解決する方法は3つあります。

まずCoin::Flip、静的メンバー関数を作成します。

#include <string>
#include <iostream>

typedef std::string (*Flipper)(); // Function pointer typedef

class Coin
{
public:
    Coin() {}

    // Static member function. A pointer to a static member function can be
    // held in a regular function pointer.
    static std::string Flip ()
    {
        srand(23);
        int side = rand() % 2 + 1;
        return (side == 1) ? "heads." : "tails.";
    }
};

std::string Flip(Flipper flipper)
{
    return flipper();
}

int main()
{
    std::cout << "Flipping a coin..." << std::endl;
    std::string output = Flip(&Coin::Flip);
    std::cout << "The coin came up " << output << std::endl;
    return 0;
}

Coin::Flip非静的メンバー関数である必要がある場合は、メンバー関数ポインターとともにインスタンスをに渡すことができますCoinFlip

#include <functional>
#include <string>
#include <iostream>

class Coin
{
public:
    Coin() {}

    // Non-static member function.
    std::string Flip ()
    {
        srand(23);
        int side = rand() % 2 + 1;
        return (side == 1) ? "heads." : "tails.";
    }
};

typedef std::mem_fun_ref_t<std::string, Coin> Flipper;

// We need the Coin instance as well as the member function pointer.
std::string Flip(Coin& coin, Flipper flipper)
{
    // Invoke the flipper member function on the coin instance
    return flipper(coin);
}

int main()
{
    // Since we're using a non-static member function, we need an instance
    // of Coin.
    Coin coin;
    std::cout << "Flipping a coin..." << std::endl;
    std::string output = Flip(coin, mem_fun_ref(&Coin::Flip));
    std::cout << "The coin came up " << output << std::endl;
    return 0;
}

最後に、Flipperファンクターが任意の種類のオブジェクト(Coinだけでなく)のメンバー関数であり、free関数をテンプレートにしたくない場合は、最近のC++11の一部である必要がありFlipます。標準。は、任意の種類の呼び出し可能なターゲット(フリー関数、メンバー関数、関数オブジェクトなど)で機能する汎用ポリモーフィック関数ラッパーです。C++ 11を使用できない場合、Boostライブラリには同等のとがあります。std::functionstd::bindstd::functionboost::functionboost::bind

#include <functional>
#include <string>
#include <iostream>

class Coin
{
public:
    Coin() {}

    // Non-static member function.
    std::string Flip ()
    {
        srand(23);
        int side = rand() % 2 + 1;
        return (side == 1) ? "heads." : "tails.";
    }

    // Static member function.
    static std::string StaticFlip()
    {
        srand(23);
        int side = rand() % 2 + 1;
        return (side == 1) ? "heads." : "tails.";
    }
};

// Flipper is a generic function object wrapper that works with free functions,
// function objects, static member functions, and non-static member functions.
typedef std::function<std::string ()> Flipper;

std::string Flip(Flipper flipper)
{
    return flipper();
}

int main()
{
    // Example with non-static member function
    Coin coin;

    // Bind a Coin instance along with a Coin::Flip member function pointer.
    Flipper flipper1 = std::bind(&Coin::Flip, &coin);

    std::cout << "Flipping a coin..." << std::endl;
    std::string output = Flip(flipper1);
    std::cout << "The coin came up " << output << std::endl;

    // Example with static member function
    Flipper flipper2 = &Coin::StaticFlip;
    std::cout << "Flipping a coin..." << std::endl;
    output = Flip(flipper2);
    std::cout << "The coin came up " << output << std::endl;

    return 0;
}
于 2012-07-10T16:17:14.403 に答える