简体   繁体   English

这是正确的C ++ 0x代码吗?

[英]Is this correct C++0x code?

Tried this in GCC 4.6 and it compiles and links, but gives a "bus error" message on runtime on MacOS. 在GCC 4.6中尝试了它并编译和链接,但在MacOS上运行时发出“总线错误”消息。 VS2010 doesn't even compile it. VS2010甚至没有编译它。

But the question is, should this actually work in standard C++0x? 但问题是,这应该在标准C ++ 0x中实际工作吗?

#include <cstdio>
int (*main)()=[]()->int{printf("HEY!\n");return 0;};

Yes, what it's trying to do is to define "main" as a lambda function. 是的,它试图将“main”定义为lambda函数。

This is not a valid C++ program, because the symbol main is not defined to be a function, but rather a pointer to function. 这不是一个有效的C ++程序,因为符号main未定义为函数,而是指向函数的指针。 That's why you get segmentation fault -- the runtime is attempting to execute a pointer. 这就是你得到分段错误的原因 - 运行时试图执行指针。

No, this is not correct. 不,这不正确。

Main is a special function and there are strict requirements for it (even more strict than a regular function), but you are also making some confusion between what is a function and what is a pointer to a function. Main是一个特殊的函数,对它有严格的要求(甚至比常规函数更严格),但是你在函数和函数指针之间也会产生一些混淆。

The logical problem is that there is a difference between a function and a variable holding a pointer to a function (what you want main to be). 逻辑问题是函数和持有指向函数的指针的变量之间存在差异(你想要的是什么)。 A function has a fixed address in memory, so to call a function that address is simply called. 函数在内存中有一个固定的地址,因此调用一个简单调用地址的函数。 A pointer to a function points to an address in memory, so to call the function you need first to read what the pointer is pointing to and then call that address. 一个指向函数指向内存中的地址,所以打电话给你首先需要读什么指针指向,然后调用该地址的功能。

A pointer to function has a different level of indirection from a function. 指向函数的指针具有与函数不同的间接级别。

The syntax is the same... ie if x is a pointer to a function you can write x(42) , but still the generated machine code is different if x is instead a function (in the case of a pointer a value must be looked up and the call address is determined at run time, with a function the address is fixed - up to relocation - and is determined at link time). 语法是相同的...即,如果x是指向函数的指针,则可以编写x(42) ,但如果x是函数,则生成的机器代码仍然不同(在指针的情况下,值必须是查找并在运行时确定呼叫地址,其中一个函数是地址固定 - 直到重定位 - 并在链接时确定。

For portability among compilers and standard library implementations, printf() must be std::printf() when #including <cstdio> . 对于编译器和标准库实现之间的可移植性,当#including <cstdio>时,printf()必须是std :: printf()。 And the other stuff about the invalid main(). 以及关于无效main()的其他内容。

Now, it should not even compile. 现在,它甚至不应该编译。 The lambda expression yields a type (a functor). lambda表达式产生一个类型(仿函数)。 There is no implicit conversion from type to function pointer. 没有从类型到函数指针的隐式转换。

Depending on compiler, main function can have C++ or C linkage (it is implementation defined). 根据编译器, main函数可以具有C ++或C链接(它是实现定义的)。 Lambda expression returns C++ type with function call operator, thus C++ linkage. Lambda表达式返回带有函数调用操作符的C ++类型,因此C ++链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM