Object lifetime is usually tied to scope but when an object is created inside a function call things become a bit blurrier (at least to me). Check the code below:
#include <iostream>
#include <string>
using namespace std;
struct ins
{
ins() : _num(++num) { cout << "ctor " << _num << endl; }
~ins() { cout << "dtor " << _num << endl; }
int get() { return _num; }
static int num;
int _num;
};
int ins::num = 0;
ins geti() { return {}; }
void usei(int i) { cout << "using " << i << endl; }
int main()
{
usei(geti().get());
cout << endl;
usei(geti().get());
}
Output
ctor 1
using 1
dtor 1
ctor 2
using 2
dtor 2
Long story short, I want to know if there's a way to "calculate" the lifetime of objects in such cases. How would I know to what function each object is tied to? my current mental model of what's going on does not help me much.