c++ - 为什么我们需要&在boost中绑定(bind)成员函数?

标签 c++ boost

在 boost 文档中:

Binding member functions can be done similarly. A bound member function takes in a pointer or reference to an object as the first argument. For instance, given:

    struct xyz
    {
        void foo(int) const;
    };
    xyz's foo member function can be bound as:

    bind(&xyz::foo, obj, arg1) // obj is an xyz object

为什么我们需要 &xyz::foo,而不仅仅是 xyz::foo?

int f(int a, int b)
{
    return a + b;
}
std::cout << bind(f, 1, 2)() << std::endl;  

这样,我们就不用&了。

最佳答案

address-of 运算符(即 &)是获取指向成员函数的指针所必需的。对于 non-member function它是可选的,因为 function-to-pointer隐式转换。

A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the function-to-pointer implicit conversion, the address-of operator is optional:

关于c++ - 为什么我们需要&在boost中绑定(bind)成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45852416/

相关文章:

c++ - 使用 boost 异步发送和接收自定义数据包?

c# - 如何在 C# 和非托管 C++ 库之间传递对指针的引用?

C++ 类型无法解析错误

c++ - 如何将 C++ 指针传递给 Fortran?

c++ - 使用 boost asio 时如何获取 UDP 远程端点?

c++ - boost lambda 问题

c++ - 正确初始化多维 vector

c++ - 理解指针和本地范围

c++ - 强制评估 constexpr 静态成员

c++ - boost::uuids::uuid 作为 std::unordered_map 中的键?