C++ function types

南楼画角 提交于 2019-11-28 09:46:59
aschepler

Here's the relevant paragraph from the Standard. It pretty much speaks for itself.

8.3.5/10

A typedef of function type may be used to declare a function but shall not be used to define a function (8.4).

Example:

typedef void F();
F  fv;         // OK: equivalent to void fv();
F  fv { }      // ill-formed
void fv() { }  // OK: definition of fv

A typedef of a function type whose declarator includes a cv-qualifier-seq shall be used only to declare the function type for a non-static member function, to declare the function type to which a pointer to member refers, or to declare the top-level function type of another function typedef declaration.

Example:

typedef int FIC(int) const;
FIC f;               // ill-formed: does not declare a member function
struct S {
  FIC f;             // OK
};
FIC S::*pm = &S::f;  // OK

In your case, std_fun_1 and std_fun_2 are identical objects with identical type signatures. They are both std::function<int(int)>, 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!