With the advent of C ++ 11, new features were added to C ++ and I didn’t have an understanding of how the list of objects from a function is returned?

There is a code:

class SuperObject { SuperObject(int a_) : a(a_) { } private: int a = 0; }; std::list<SuperObject> collectSuperObjects() { std::list<SuperObject> tmp; // do something with tmp return tmp; } 

Questions:

  1. Do I understand correctly that the compiler will not add any constructors to SuperObject in our case, since user wrote his?
  2. How will the result of the collectSuperObjects function be returned?

First of all, I'm interested in the situation with MSVC 2013.

  • Explain what constructors you have in mind. - ixSci
  • c++11 not so new. Currently relevant c++14 . - αλεχολυτ
  • @alexolut Let's be honest, even C++11 not fully supported by everyone, and you shouldn’t talk about C++14 ;) - sys_dev
  • @sys_dev by no means. Clang and gcc already support the rudiments of c ++ 17. Here at MS with it not so, as far as I know. - αλεχολυτ
  • @alexolut does not start supporting the next standard when the previous one is fully covered :) However, in the GCC from C ++ 0x, only "Minimal support for garbage collection and reachability-based leak detection" is not covered at the moment, the authenticity of which in C ++ under some doubt ... - D-side

2 answers 2

1) Yes, there will be no default constructor, in C ++ 03 it was the same.
2) tmp returned by moving, while the elements of the list are not copied.
(In C ++ 03, there would be an NRVO, but you would still need a copy constructor).

To add an item to the list, you can use tmp.emplace_back(1); .

  • one
    So now it will be NRVO, there will be no movement here (most likely) - ixSci
  • @ixSci probably can do the entire call in general. The main thing is that there will be no copying. - Abyx

In the case of SuperObject copy and move constructors will be generated, as well as the corresponding copy and move operators. No other constructors will be generated. In order for the compiler to stop generating the copy constructor, it’s enough to write your own. It is even easier to prohibit the generation of relocation, just implement one of the following list:

  • Copy Constructor
  • Motion constructor
  • Destructor
  • Copy operator
  • Move operator

But why such a difference with the copy constructor? Because the old code needs to be maintained. By the way, the above is not written the whole truth about the generation of the copy constructor, in the following cases it will be automatically generated as deleted ( = delete ) if the user added:

  • Motion constructor
  • Move operator

Thus, the standard almost equalizes two constructors, only the destructor cannot yet forbid the copy constructor - it would break a lot of code. But this is already declared as deprecated , so in the future, the copy constructor will not be generated if the class has a destructor or a copy operator (custom, I mean):

[depr.impldec]

If you are a user, you have been given a copyright or a copyright statement. If you are a user, you must be able to use a copyright code (12.4, 12.8). These could not be deleted (8.4).

About the 2 part of the question Abyx wrote already - it will be either NRVO, or the list will be moved. Items will not move.