1

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.

Community
  • 1
  • 1
user64953
  • 723
  • 1
  • 6
  • 10
  • Q1 difficult it is. Q2 defined in the standard section 12 it is. – NathanOliver Oct 21 '15 at 13:08
  • @BoPersson: I do not think this is a duplicate. The other question asked **when** compiler should generate special members, this one asks whether **implementation** is available and/or defined by standard. Anyway I've added an answer [there](http://stackoverflow.com/a/33260855/3545273) – Serge Ballesta Oct 21 '15 at 13:42
  • @Serge - The other question/answer refers to the standard §§ for all the rules. At least that definitely answers Q2 . – Bo Persson Oct 21 '15 at 13:46
  • @BoPersson: I do not agree with you. My understanding is that OP does not ask what is required for the special members but whether the **implementation** source code is available, and defined by the standard. The latter part could be partly answered by [What exactly is the “as-if” rule?](http://stackoverflow.com/a/15718279), but IMHO it still requires the precision that compilers are not required to generate source code for the special members. – Serge Ballesta Oct 21 '15 at 13:55
  • @SergeBallesta if you could write down your answer here, I will accept it as a best answer. – user64953 Oct 22 '15 at 14:06

2 Answers2

0

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Well, I'll start with the default constructor. Maybe others can edit this and add info for other constructors etc. SO was meant to be a collaborative effort.

Default constructor.

Relevant standardese:

C++14 §12.1/5

[…] The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement. If that user-written default constructor would be ill-formed, the program is ill-formed. If that user-written default constructor would satisfy the requirements of a constexpr constructor (7.1.5), the implicitly-defined default constructor is constexpr. Before the defaulted default constructor for a class is implicitly defined, all the non-user-provided default constructors for its base classes and its non- static data members shall have been implicitly defined. [Note: An implicitly-declared default constructor has an exception-specification (15.4). An explicitly-defaulted definition might have an implicit exception- specification, see 8.4. —end note ]

In effect, the generated default constructor for a class T looks like

T(){}

or

constexpr T(){}

depending on whether it can be constexpr.

One important consequence is that members of built-in types are not initialized, and are therefore left with indeterminate values. Except for char types it's formally UB to use such values, although in practice, on modern machines it's just arbitrary values.

One important consideration is that this does not affect value initialization, e.g. the effect of T(). It doesn't use the generated default constructor and ends up zero-initializing members of built-in types.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331