C++ function types

后端 未结 2 1176
攒了一身酷
攒了一身酷 2020-12-10 01:11

I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function):

typedef in         


        
2条回答
  •  一个人的身影
    2020-12-10 01:58

    In your case, std_fun_1 and std_fun_2 are identical objects with identical type signatures. They are both std::function, and can both hold function pointers or callable objects of type int(int).

    pf is a pointer to int(int). That is, it serves the same basic purpose as std::function, but without the machinery of that class or support for instances of callable objects.

    Similarly, std_fun_3 and std_fun_4 are identical objects with identical type signatures, and can both hold function pointers or callable objects of type int(int) const.

    Also similarly, pfc is a function pointer of type int(int) const, and can hold pointers to functions of that type, but not instances of callable objects.

    But f and fc are function declarations.

    The line:

    Signature fc;
    

    Is identically equivalent to:

    int fc(int) const;
    

    Which is a declaration for a function named fc of type int(int) const.

    There's nothing strange going on here. You've simply happened upon syntax you probably already understand, from a perspective you're not accustomed to.

提交回复
热议问题