Scope of temporaries as function arguments

Ali Çehreli acehreli at yahoo.com
Fri Jun 28 09:44:02 PDT 2013


On 06/28/2013 09:01 AM, monarch_dodra wrote:

 > And I'm 99% sure C++ doesn't have this problem...

+1%. :)

I just finished checking. No, C++ does not have this problem. But there 
is the following related issue, which every real C++ programmer should 
know. ;)

 
http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#BestPractices

Ali

P.S. The C++ program that I have just used for testing:

#include <iostream>
#include <stdexcept>

using namespace std;

int callme()
{
     throw runtime_error("");
     return 0;
}

struct S
{
     int i_;

     S(int i)
         :
         i_(i)
     {
         cout << "constructing: " << i_ << " at " << this << '\n';
     }

     S(const S & that)
     {
         cout << "copying: " << that.i_ << " to " << this << '\n';
         i_ = that.i_;
     }

     ~S()
     {
         cout << "destroying: " << i_ << " at " << this << '\n';
     }
};

void foo(int i, S s)
{
    s.i_ = 2;
}

int main()
{
     S s = S(1);

     try {
         foo(callme(), s);

     } catch (...) {
         cout << "caught\n";
     }
}



More information about the Digitalmars-d-learn mailing list