C++ 函数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17446220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ function types
提问by marton78
I have a problem understanding function types (they appear e.g. as the Signature
template parameter of a std::function
):
我在理解函数类型时遇到问题(例如它们作为 a 的Signature
模板参数出现std::function
):
typedef int Signature(int); // the signature in question
typedef std::function<int(int)> std_fun_1;
typedef std::function<Signature> std_fun_2;
static_assert(std::is_same<std_fun_1, std_fun_2>::value,
"They are the same, cool.");
int square(int x) { return x*x; }
Signature* pf = square; // pf is a function pointer, easy
Signature f; // but what the hell is this?
f(42); // this compiles but doesn't link
The variable f
can not be assigned, but can be called. Weird. What is it good for, then?
变量f
不能被赋值,但可以被调用。奇怪的。那它有什么用呢?
Now if I const-qualify the typedef, I can still use it to build further types but apparently for nothing else:
现在,如果我对 typedef 进行常量限定,我仍然可以使用它来构建更多类型,但显然没有别的:
typedef int ConstSig(int) const;
typedef std::function<int(int) const> std_fun_3;
typedef std::function<ConstSig> std_fun_4;
static_assert(std::is_same<std_fun_3, std_fun_4>::value,
"Also the same, ok.");
ConstSig* pfc = square; // "Pointer to function type cannot have const qualifier"
ConstSig fc; // "Non-member function cannot have const qualifier"
What remote corner of the language have I hit here? How is this strange type called and what can I use it for outside of template parameters?
我在这里碰到了语言的哪个偏远角落?这个奇怪的类型是如何调用的,我可以在模板参数之外使用它做什么?
回答by 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-seqshall 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
8.3.5/10
函数类型的 typedef 可用于声明函数,但不得用于定义函数(8.4)。
例子:
typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void fv() { } // OK: definition of fv
声明符包含cv-qualifier-seq的函数类型的 typedef应仅用于声明非静态成员函数的函数类型,声明指向成员的指针所指的函数类型,或声明顶部另一个函数 typedef 声明的级别函数类型。
例子:
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
回答by greyfade
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)
.
在您的情况下,std_fun_1
和std_fun_2
是具有相同类型签名的相同对象。它们都是std::function<int(int)>
,并且都可以保存函数指针或类型的可调用对象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.
pf
是指向 的指针int(int)
。也就是说,它的基本目的与 相同std::function
,但没有该类的机制或对可调用对象实例的支持。
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
.
类似地,std_fun_3
和std_fun_4
是具有相同类型签名的相同对象,并且都可以保存函数指针或类型为 的可调用对象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.
同样类似地,pfc
是一个类型为 的函数指针int(int) const
,可以保存指向该类型函数的指针,但不能保存可调用对象的实例。
But f
and fc
are function declarations.
但是f
andfc
是函数声明。
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
.
这是一个名为fc
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.
这里没有什么奇怪的。从您不习惯的角度来看,您只是偶然发现了您可能已经理解的语法。