VC2013: function from bind not compiling

前端 未结 1 1143
[愿得一人]
[愿得一人] 2020-12-03 19:41

Here is a short sample:

class A {};

class S {};

class B
{
public:
    typedef std::function  property_getter_t;
    typedef std         


        
相关标签:
1条回答
  • 2020-12-03 20:25

    I would normally quote the standard, but the standardese here is particularly dense, so I'll just explain what's happening.

    std::bind does something special when you pass an argument to be bound if that argument is a function object returned by a previous call to std::bind. Suppose you do:

    auto f = std::bind(foo, std::placeholders::_1);
    auto g = std::bind(bar, f, std::placeholders::_1);
    

    Then invoking g(a) is roughly equivalent to

    bar(foo(a), a)
    

    rather than

    bar(f, a)
    

    Demo.

    That is, if you pass something returned by bind to bind as an argument to be bound, what's going to end up being passed to the function you are binding is the result of invoking that bind expression. It's a sort of composition.

    However, in your code, you actually don't want the composition to take place. (This composition is what caused g++ to emit 320+ lines of errors when I tried to call your fn_gg on an A. Clang with libc++ printed only 10 lines, thankfully.) You want fn_g to be treated just like an ordinary argument. So you can wrap it in a std::function:

    std::function<bool (const std::shared_ptr<S>&, A&)> fn_g = std::bind(getter, this, std::placeholders::_1, std::placeholders::_2);
    
    0 讨论(0)
提交回复
热议问题