9

Why does rvalue optimization not occur in classes with constructor with universal reference arguments?

http://coliru.stacked-crooked.com/a/672f10c129fe29a0

#include <iostream>

 template<class ...ArgsIn>
struct C {

  template<class ...Args>
  C(Args&& ... args) {std::cout << "Ctr\n";}        // rvo occurs without &&

  ~C(){std::cout << "Dstr\n";}
};

template<class ...Args> 
auto f(Args ... args) {
    int i = 1;
  return C<>(i, i, i);
}

int main() {
  auto obj = f();
}

Output:

Ctr
Ctr
Dstr
Ctr
Dstr
Dstr
tower120
  • 5,007
  • 6
  • 40
  • 88
  • 3
    *Universal reference* is a term coined by one particular author and with which other people disagree… I personally believe that it causes more confusion than it helps, you should try to understand how type deduction works, as this is the answer to the question here. – David Rodríguez - dribeas Jul 24 '14 at 04:38
  • 1
    using g++ 4.8.3 I get `Ctr` `Dstr` for both `C(Args... args)` and `C(Args &&... args)` . What is your compiler and version (and flags)? – M.M Jul 24 '14 at 04:40
  • 1
    @MattMcNabb "g++ -std=c++1y -O3 -Winline -Wextra -pthread -pedantic-errors" version 4.9. GCC 4.8.1 also not do rvo http://coliru.stacked-crooked.com/a/d2ddb81f9ed2d217 – tower120 Jul 24 '14 at 04:42
  • is `auto f()` legal without specifying `-> type` ? – M.M Jul 24 '14 at 04:47
  • … actually I don't think that type deduction has much to do here… the compiler is not doing an **optimization** (which as all optimizations is not mandatory) and is opting to use a *move-constructor* (generated out of the variadic args constructor) instead of eliding the copy altogether. Small tweaks to the source make RVO kick in or fail (for example, providing a move constructor yourself makes RVO kick in (?!?!)… – David Rodríguez - dribeas Jul 24 '14 at 04:47
  • 1
    @MattMcNabb: `auto f() {` is (will be) legal C++14, and some compilers already support it. – David Rodríguez - dribeas Jul 24 '14 at 04:47
  • 1
    @DavidRodriguez enabling `cout` for move constructor, I still only get `Ctr` `Dstr` for both versions. (adding in -fno-elide-constructors gets `Ctr` `move` `Dstr` `move` `Dstr` `Dstr`) – M.M Jul 24 '14 at 04:48
  • @MattMcNabb Yes, sure. Since C++1y. But you can replace it with concrete type. It does not change anything. – tower120 Jul 24 '14 at 04:48
  • It seems dependent on whether you have a [move constructor overload](http://coliru.stacked-crooked.com/a/16eece557520213e). – T.C. Jul 24 '14 at 04:48
  • @T.C. Nice. But WHYY??? – tower120 Jul 24 '14 at 04:49
  • 1
    @DavidRodríguez-dribeas When I add move constructor http://coliru.stacked-crooked.com/a/16eece557520213e, compiler not call it due to this http://stackoverflow.com/a/8411703/1559666 ? – tower120 Jul 24 '14 at 04:56
  • What is "rvalue optimization"? – juanchopanza Jul 24 '14 at 05:58
  • @tower120: That link explains what RVO is, but doesn't explain why the compipler can't call it – Mooing Duck Jul 25 '14 at 00:29
  • @MooingDuck That was the question, not explanation. – tower120 Jul 26 '14 at 05:18

1 Answers1

12

I believe that the problem is that instantiations of

template<class ...Args>
C(Args&& ... args) {std::cout << "Ctr\n";}  

are not copy/move constructors as far as the language is concerned and therefore calls to them cannot be elided by the compiler. From §12.8 [class.copy]/p2-3, emphasis added and examples omitted:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).

A non-template constructor for class X is a move constructor if its first parameter is of type X&&, const X&&, volatile X&&, or const volatile X&&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).

In other words, a constructor that is a template can never be a copy or move constructor.

The return value optimization is a special case of copy elision, which is described as (§12.8 [class.copy]/p31):

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects.

This allows implementations to elide "copy/move construction"; constructing an object using something that's neither a copy constructor nor a move constructor is not "copy/move construction".

Because C has a user-defined destructor, an implicit move constructor is not generated. Thus, overload resolution will select the templated constructor with Args deduced as C, which is a better match than the implicit copy constructor for rvalues. However, the compiler can't elide calls to this constructor, as it has side effects and is neither a copy constructor nor a move constructor.

If the templated constructor is instead

template<class ...Args>
C(Args ... args) {std::cout << "Ctr\n";} 

Then it can't be instantiated with Args = C to produce a copy constructor, as that would lead to infinite recursion. There's a special rule in the standard prohibiting such constructors and instantiations (§12.8 [class.copy]/p6):

A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cv-qualified) X and either there are no other parameters or else all other parameters have default arguments. A member function template is never instantiated to produce such a constructor signature.

Thus, in that case, the only viable constructor would be the implicitly defined copy constructor, and calls to that constructor can be elided.

If we instead remove the custom destructor from C, and add another class to track when C's destructor is called instead:

struct D {
    ~D() { std::cout << "D's Dstr\n"; }
};

template<class ...ArgsIn>
struct C {
  template<class ...Args>
  C(Args&& ... args) {std::cout << "Ctr\n";}
  D d;
};

We see only one call to D's destructor, indicating that only one C object is constructed. Here C's move constructor is implicitly generated and selected by overload resolution, and you see RVO kick in again.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • Can you explain how this relates to the problem? – M.M Jul 24 '14 at 05:05
  • @MattMcNabb RVO elides copy/move constructor calls. It doesn't (apparently) elide calls to something that's neither. – T.C. Jul 24 '14 at 05:07
  • OK, so the issue is arising because `Args` is being deduced as `C` – M.M Jul 24 '14 at 05:10
  • @MattMcNabb Right, and it becomes something that seems like a move constructor but really isn't. – T.C. Jul 24 '14 at 05:19
  • @T.C. So, you think this is some kind of a compiler bug? – tower120 Jul 24 '14 at 05:30
  • 2
    @tower120 No, it's not a compiler bug. This is the behavior specified by the standard. The compiler is not allowed to elide calls to the templated constructor, because it's neither a copy constructor nor a move constructor. – T.C. Jul 24 '14 at 05:30
  • @T.C. Here §12.8 [class.copy]/p2-3 said about NON-TEMPLATE constructor. Which is not our case. – tower120 Jul 24 '14 at 05:33
  • @tower120 The only kind of constructors that can be copy/move constructors is *non-template* constructors. – T.C. Jul 24 '14 at 05:34
  • @T.C. You said that compiler is not allowed to elide call to constructor, because it's not copy/move ctr. But I not say about copy elision. I talk about http://en.wikipedia.org/wiki/Return_value_optimization . So what we have? We have a single constructor and destructor. And none of copy/move constructors. So what prevents compiler from this form of optimization? – tower120 Jul 24 '14 at 05:42
  • 2
    @tower120 Return value optimization *is* copy elision. That's how it's specified - as the compiler being allowed to elide certain copy/move operations. – T.C. Jul 24 '14 at 05:45
  • +1, great answer. I said basically the same thing when closing the bug report as INVALID: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61892#c1 – Jonathan Wakely Jul 24 '14 at 09:17
  • @JonathanWakely The only thing that I understand from all of this - is that it can't be done, because Args can be C, and that can start infinite recursion :) – tower120 Jul 24 '14 at 09:51
  • 2
    Well then you've understood completely wrong. Declare copy and move constructors and you get RVO in all allowed situations, or constrain the constructor template using SFINAE so it cannot be used to copy or move `C` objects. – Jonathan Wakely Jul 25 '14 at 09:02
  • @JonathanWakely Can you show simple example of that thing with SFINAE? :) – tower120 Jul 26 '14 at 05:20