Nested bind expressions

后端 未结 2 463
终归单人心
终归单人心 2021-02-05 18:53

This is a followup question to my previous question.

#include 

int foo(void) {return 2;}

class bar {
public:
    int operator() (void) {retu         


        
2条回答
  •  深忆病人
    2021-02-05 19:28

    Looking at this the second time now, and I think I have a plausable explanation for the first error you are seeing.

    In this case, it's more helpful to look at the complete error and the template instantiations that lead up to it. The error printed by my compiler (GCC 4.4), for example, ends with the following lines:

    test.cpp:12:   instantiated from ‘decltype (c()) func(C&&) [with C = std::_Bind]’
    test.cpp:16:   instantiated from ‘void func_wrapper(C&&) [with C = int (&)()]’
    test.cpp:22:   instantiated from here
    /usr/include/c++/4.4/tr1_impl/functional:1137: error: invalid initialization of reference of type ‘int (&)()’ from expression of type ‘int (*)()’
    

    Now looking at this bottom-up, the actual error message seems correct; the types the compiler has deduced are incompatible.

    The first template instantiation, at func_wrapper, clearly shows what type the compiler has deduced from the actual parameter foo in func_wrapper(foo). I personally expected this to be a function pointer, but it is in fact a function reference.

    The second template instantiation is hardly readable. But messing around with std::bind a bit, I learned that the format of the textual representation GCC prints for a bind functor is roughly:

    std::_Bind
    

    So tearing it apart:

    std::_Bind
    // Return type: int
    // Bound value types: int (*)()
    // Target parameter types: int (&)()
    

    This is where the incompatible types start. Apparently, even though c in func_wrapper is a function reference, it turns into a function pointer once passed to std::bind, resulting in the type incompatibility. For what it's worth, std::forward doesn't matter at all in this case.

    My reasoning here is that std::bind only seems to care about values, and not references. In C/C++, there's no such thing as a function value; there's only references and pointers. So when the function reference is dereferenced, the compiler can only meaningfully give you a function pointer.

    The only control you have over this is your template parameters. You will have to tell the compiler that you're dealing with a function pointer from the start to make this work. It's probably what you had in mind anyways. To do that, explicitly specify the type you want for the template parameter C:

    func_wrapper(foo);
    

    Or the more brief solution, explicitly take the function's address:

    func_wrapper(&foo); // with C = int (*)()
    

    I'll get back to you if I ever figure out the second error. :)

提交回复
热议问题