This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

an rvalue reference question


To whom it may concern,

       I hope the appended is not a dumb question, but would appreciate very much for your help.
       This is the first time that I seek help from this site;  please let me know if I need to provide any
       further information if I should miss any.

       - Chih-Long Lin

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

$ gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.3.2/configure --enable-languages=c,c++
Thread model: posix
gcc version 4.3.2 (GCC)

$ g++ -O0 -std=c++0x 20090102.C
$ ./a.out
-- blah()
-- blah(blah const&)
-- blah()
3
foo(blah const&)
-- blah()
foo(blah&&)

======================== 20090102.C ==========================
#include <iostream>

using namespace std;

struct blah {
  static int cnt;

  blah() { ++cnt; cout << "-- blah()" << endl; }
  blah(blah const& x) { ++cnt; cout << "-- blah(blah const&)" << endl; }
  blah(blah&& x) { ++cnt; cout << "-- blah(blah&&)" << endl; }
};

int blah::cnt = 0;

void foo(blah const& x) { cout << "foo(blah const&)" << endl; }
void foo(blah&& x) { cout << "foo(blah&&)" << endl; }

int
main() {
  blah x;                              // calls blah::blah(), as expected
  blah y(x);                          // calls blah::blah(blah const&), as expected
  blah z((blah()));                 // expecting calling blah::blah() and blah::blah(blah&&) but got only blah::blah()

  cerr << blah::cnt << endl;  // expecting 4 but got 3

  foo(x);                               // calls foo(blah const&) as expected
  foo((blah()));                      // calls blah::blah() and foo(blah&&), as expected
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]