Scope of temporaries as function arguments

Maxim Fomin maxim at maxim-fomin.ru
Fri Jun 28 10:17:38 PDT 2013


On Friday, 28 June 2013 at 16:44:03 UTC, Ali Çehreli wrote:
> Ali
>
> P.S. The C++ program that I have just used for testing:

Are you sure that the code is exact translation of demonstrated D 
problem? I see difference in argument passing order and your 
version uses try-catch block.

This code

#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(S s, int i)
{
    s.i_ = 2;
}

int main()
{
     S s = S(1);
     foo(s, callme());

}

prints for me:

constructing: 1 at 0x7fffb93078d0
terminate called after throwing an instance of 
'std::runtime_error'
   what():
Aborted


More information about the Digitalmars-d-learn mailing list