简体   繁体   English

C ++:std :: bind()如何“知道”模板args是引用静态函数还是成员函数?

[英]c++ : how does std::bind() 'know' whether the template args refer to a static function or member function?

Using bind() I can do both of these: 使用bind()我可以做到这两个:

void f(int a) { }

class C {
public:
    void f (int a) { }
};

int main()
{
    auto f1 = std::bind (f,3);
    f1();
    C myC;
    auto f2 = std::bind (&C::f, &myC, 3);
    f2();
}

Presumably underneath, f1() is in someway translated to f(3), and f2() is translated to (&myC)->f (3). 大概在下面,f1()以某种方式转换为f(3),f2()转换为(&myC)-> f(3)。 I'm not too concerned with the binding of the '3' argument, but I would like to understand how bind() automagically knows that f1 should be just a straight function call whereas f2 should be a member call of an object. 我不太担心'3'参数的绑定,但是我想了解bind()如何自动知道f1应该只是一个直接函数调用,而f2应该是一个对象的成员调用。 What is the technique used to check what 'flavour' the first arg is at compile time? 在编译时检查第一个arg有什么“味道”的技术是什么? I'd like to leverage this kind of technique for my own program. 我想在自己的程序中利用这种技术。

A simple, packed approach is to use 一种简单的打包方法是使用

std::is_member_function_pointer<F>::value

where F is the type of the first argument, probably with potential reference and cv qualification removed. 其中F是第一个参数的类型,可能已删除了潜在的引用和cv限定条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM