Assuming MyClass
has an implicit constructor that takes an int
as an argument, why is there a difference between MyClass
being explicitly constructed vs. implicitly done so in terms of when the temporary gets destroyed? In one case it results in a dangling reference while in another it does not.
template <typename T>
void f(const T &x) {
cout << __PRETTY_FUNCTION__ << endl;
}
int main()
{
f(std::tuple<const MyClass &>(10));
cout << "---------------\n";
f(std::tuple<const MyClass &>(MyClass{}));
}
Output:
MyClass::MyClass(int)
MyClass::~MyClass()
void f(const T&) [with T = std::tuple<const MyClass&>]
---------------
MyClass::MyClass()
void f(const T&) [with T = std::tuple<const MyClass&>]
MyClass::~MyClass()