[Bug c++/61892] New: RVO not occurs with constructor with universal reference arguments

tower120 at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Jul 24 05:25:00 GMT 2014


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61892

            Bug ID: 61892
           Summary: RVO not occurs with constructor with universal
                    reference arguments
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tower120 at gmail dot com

I'm not sure that this is bug. But this is strange behavior, if you ask me.

LIVE: http://coliru.stacked-crooked.com/a/d11d50f611ed0cee

In the following code:

#include <iostream>

template<class ...ArgsIn>
struct C {

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

  ~C(){std::cout << "Dstr\n";}
  //C(C&&) { std::cout << "A move was made.\n"; }   // with this and universal
references in ctr, rvo occurs
};

template<class ...Args> 
auto f(Args ... args) {
  int i = 1;
  std::cout << "call "<<std::endl;
  return C<>(i, i, i);
}

int main() {
  std::cout << "Hello World!\n";
  auto obj = f();
}

OUTPUT:

g++ -std=c++1y  -O3 -Winline -Wextra -pthread -pedantic-errors main.cpp -lm  &&
./a.out
Hello World!
call 
Ctr
Ctr
Dstr
Ctr
Dstr
Dstr

=========================================================

1) If I have constructor with "universal" references I need to have ANY move
constructor, so rvo happened. 
2) Moreover, when rvo not happens, it construct object with a wrong number of
arguments:
http://coliru.stacked-crooked.com/a/e3ce8882c68dbef2

=========================================================


P.S.
http://stackoverflow.com/questions/24925137/c-universal-reference-in-constructor-and-return-value-optimization-rvo/24925451#comment38729738_24925137
someone claims that with gcc 4.8.3 this problem not exists. I don't have that
compiler at hand, so I can't check that.



More information about the Gcc-bugs mailing list