This is the mail archive of the gcc-bugs@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]

[Bug c++/18272] New: weird behaviour on temporary return values


If a function returns an auto_ptr, it is impossible to dereference the return
value on the fly and then use it, the temporary object seems to be destroyed
on the next line.

However, if there is a subsequent call to this function that releases another
return value, it works.

#include <iostream>
#include <vector>

using std::cout;
using std::cerr;
using std::endl;
using std::vector;
using std::exception;
using std::auto_ptr;

// function creating auto_ptrs
auto_ptr<vector<int> >
ert()
{
  auto_ptr<vector<int> > res(new vector<int>);

  res->assign(7, 20);
  return res;
}

int main(int argc, char ** argv)
{
  try {
    auto_ptr<vector<int> > res1 = ert(); // OK
    vector<int>& res2 = *res1;           // OK
    vector<int> *res3 = ert().get();     // NOK
    vector<int>& res4 = *(ert());        // NOK if res5 not created afterwards
    vector<int>& res5 = *(ert().release());// OK
    
    cout << Size " << res1->size()
         << " | " << res2.size()
         << " | " << res3->size()
         << " | " << res4.size()
         << endl;
  }
  catch(exception& exc) {
    cerr << exc.what() << endl;
    return -3;
  }
  catch(...) {
    cerr << "unexpected exception" << endl;
    return -5;
  }

  return 0;
}

-- 
           Summary: weird behaviour on temporary return values
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: philippe dot haution at mines-paris dot org
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18272


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