c++ - 没有函数模板如何使用mem_fun_ref?

标签 c++

我尝试使用 mem_fun_ref 将对对象的成员函数的引用发送到另一个函数,但我收到错误 C2064:term 无法计算为采用 0 个参数的函数。

我没有在示例中反射(reflect)这一点,但我需要将 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;
}

最佳答案

您应该阅读 static member functions ,以及member function pointers 。您可以通过三种方法解决您的问题。

首先是使 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需要是一个非静态成员函数,您可以将Coin实例传递给Flip,同时成员函数指针:

#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)的成员函数,并且您不希望Flip自由函数作为模板,您需要 std::functionstd::bind这是最近的 C++11 标准的一部分。 std::function 是一个通用的多态函数包装器,适用于任何类型的可调用目标:自由函数、成员函数、函数对象等。如果您不能使用 C++11, Boost 库具有等效项 boost::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;
}

关于c++ - 没有函数模板如何使用mem_fun_ref?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11416586/

相关文章:

c++ - 如何更改 shared_ptr 中的指针而不失去删除内存的能力?

c++ - 如何以编程方式获取 Windows 搜索历史记录?

c++ - 用户友好的 CMake 库路径 (CCMake)

c++ - 计时器与事件 : which one is preferable for Asynchronous processing?

c++ - 如何使用静态成员函数创建一个矩阵,然后可以使用运算符重载打印该矩阵?

c++ - 在 C/C++ 中包含未使用的头文件是否会影响性能?

c++ - 是否可以配置 UDP 多播套接字以便可以调用 write() 而不是 sendto()?

c++ - CMake 属性和扩展生成器表达式

C++ 正则表达式不理解

c++ - 用于非常特殊情况的快速点积