如何知道特殊成员函数实现编译器自动生成? [英] How to know special member functions implementation compilers automatically generated?

查看:185
本文介绍了如何知道特殊成员函数实现编译器自动生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特殊成员函数 - 维基百科,自由的百科全书说,下面列出的特殊成员函数将




  • 默认构造函数

  • 复制构造函数

  • 移动构造函数

  • 复制赋值运算符

  • 移动赋值运算符

  • >


Q1。我如何看到编译器自动生成的特殊成员函数的实现?



Q2。实现依赖于编译器吗?或者是由C ++标准定义?



EDIT - 我的问题是如何编译器生成函数,而不是 >。我想知道编译器生成的源代码尽可能正确。我不认为这个问题是重复的 c ++ - 编译器何时提供类的特殊成员的定义? - 堆栈溢出

解决方案

你不知道编译器如何实现特殊成员。更确切地说,普通编译器不会直接显示给你。你可以想象一个编译器可以明确地生成这些特殊成员的源代码,但它不是必需的,我不知道是什么。



指定:




  • 当这些成员应由编译器自动生成(在此问题中回答)



  • 这是怎么从来没有指定的,因为1.9程序执行[intro.execution]§1说

p>


...符合实现需要模拟抽象机器的可观察行为,如下所述。


(强调我的)。



as-if规则


这个规定有时被称为as-if规则,可以自由地忽略这个
国际标准的任何要求,只要结果好像已经遵守了要求,只要可以从程序的
可观察的行为确定。例如,如果实际实现可以
推导出其值未被使用并且没有产生影响程序的可观察行为的副作用,则实际实现不需要评估表达式的一部分。



Special member functions - Wikipedia, the free encyclopedia says that special member functions listed below will be automatically generated if conditions are met.

  • Default constructor
  • Copy constructor
  • Move constructor
  • Copy assignment operator
  • Move assignment operator
  • Destructor

Q1. How can I see the implementation of special member functions which compilers automatically generated?

Q2. Does the implementations depend on compilers? Or is it defined by C++ standard?

EDIT - My questions is how the compilers generate the functions, not when. I want to know the source code which compilers generate as correctly as possible. I don't think this question is duplicate of c++ - When does the compiler provide definitions for the special members of a class? - Stack Overflow.

解决方案

You cannot know how exactly how the compiler implements special members. More exactly, common compilers do not directly show it to you. You could imagine that a compiler could explicitely generate the source for those special members, but it is not required, and I do not know any that does it.

The standard only specify:

  • when those members should be automatically generated by the compiler (answered in this question)
  • what those automatically generated members shall do

The how is never specified because 1.9 Program execution [intro.execution] §1 says

...conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

(emphasize mine).

and a note defines this as the as-if rule:

This provision is sometimes called the "as-if" rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

这篇关于如何知道特殊成员函数实现编译器自动生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆