英文:
How to find out which functions the compiler generated?
问题
我知道关于编译器生成的函数、三大法则和五大法则。在实际应用场景中,确切地确定编译器生成的函数(构造函数、赋值操作符、析构函数)中哪些是由编译器实际创建的可能并不容易。
有没有办法列出特定类的编译器生成函数?
我主要关注Visual Studio 2019和Xcode,但通用的解决方案会更受欢迎。
英文:
I know about compiler-generated functions, the rule of three and the rule of five. In real-world scenarios, it may not be trivial to figure out exactly which of the compiler-generated functions (constructors, assignment operators, destructor) were actually created by the compiler.
Is there any way to list the compiler-generated functions for a specific class?
I am primarily interested in Visual Studio 2019 and Xcode, but a generic solution would be even more welcome.
答案1
得分: 15
规则很复杂。我将引用来自Howard Hinnant的演示文稿的表格,该表格摘自另一个答案。
这里的道德是,一个好的实践是不依赖于编译器的隐式声明,而是根据您的需求明确声明每个特殊成员(默认或删除)。
英文:
The rules are complicated. I will steal from another answer which quotes a table from Howard Hinnant's presentation.
The moral here is that a good practice is to not rely on compiler implicit declares and explicitly declare every special member (as defaulted or deleted, depending on your needs)
答案2
得分: 7
"是否有办法列出特定类的编译器生成的函数?"
当然有。在Linux(以及其他Unix系统)上,您可以使用nm
、readelf
和objdump
对生成的目标文件/库/可执行文件进行反汇编,以检查导出的符号(以及更多内容)。
在Windows上也有类似的工具,我知道,但那不是我常用的平台,所以很抱歉,我无法提供确切的工具名称。
英文:
> "Is there any way to list the compiler-generated functions for a specific class?"
Of course there is. On Linux (and other Unix systems) you can use nm
, readelf
and objdump
on the generated object files/libraries/executable to disassemble them and inspect any exported symbols (and much more).
There are similar tools on Windows, I know, but that's not a platform I work much with, so unfortunately I cannot name exact tool names there.
答案3
得分: 1
这目前只是一个部分回答。
Visual Studio 2019
构造函数
在定义类对象时,Visual Studio的智能感知功能会显示可用的构造函数,包括编译器生成的和您自己创建的:
不幸的是,这个信息并不总是自动显示出来。为了使其在上面的截图中显示出来,我不得不在括号内输入一些内容,因此有逗号。
英文:
This is currently only a partial answer.
Visual Studio 2019
Constructors
When defining a class object, Visual Studio's IntelliSense function shows the available constructors, both compiler-generated and your own:
This information does not always come up, unfortunately. To get it to work for the screenshot above, I had to type something in the parentheses, hence the comma.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论