$ cat -n cons.cpp
1 #include <iostream>
2
3 using namespace std;
4
5 int return1() {
6 return 1;
7 }
8
9 int main() {
10 cout<< return1.m_one << endl;
11 return 0;
12 }
$ g++ cons.cpp
cons.cpp: In function 'int main()':
cons.cpp:10: error: request for member 'm_one' in 'return1',
which is of non-class type 'int ()()'
$
Run Code Online (Sandbox Code Playgroud)
也许这是编译器特定的,但是int ()()如上面的g ++报告的那样,是否存在额外的括号对的一些意义/含义?
函数指针就是int (*)().int ()()是不合法的语法,但我可以看到类型漂亮的打印机如何输出它的功能类型.
这种语法实际上意味着什么作为类型声明符,如果它是合法的,是:
function taking no arguments
??
int ()()
??? ??
and returning int(), i.e. a function that takes no argument and returns int.
Run Code Online (Sandbox Code Playgroud)
但是在C和C++中,函数被禁止直接返回函数,而是必须返回指向函数的指针.同样,你不能直接返回一个数组(int ()[10])
实际拼写函数类型的方式没有这些括号中的一个.例如,当声明参数和返回值时,这是合法的std::function
?????
std::function< int() > foo = []() -> int { return 1; };
Run Code Online (Sandbox Code Playgroud)
这种语法的想法是这样的:
int左侧意味着我的返回类型为int因此,如果该函数已被声明为
int return1(int a)
Run Code Online (Sandbox Code Playgroud)
将讨论错误消息int ()(int).
但是函数类型的表示方式确实取决于编译器和版本.例如GCC 4.5.1,我刚刚尝试过,int()因为你建议更直观.
| 归档时间: |
|
| 查看次数: |
413 次 |
| 最近记录: |