I am a bit confused about new rules about copy elision and actually I am not even sure if it applies in this case. I have this:
template <typename T> struct foo {
T t;
foo(const T& t) : t(t) {}
~foo() { std::cout << "destructor \n"; }
}
template <typename T> foo<T> make_foo(const T& t) { return {t}; }
where make_foo
is only to allow deducing the parameter (in the real code t
is a lambda, but I left it out here for the sake of simplicity, or rather for the sake of confusion, sorry for that) as in
auto x = make_foo(123);
Now I need to be absolutely sure that foo
s destructor is called exactly once: when x
goes out of scope. I am afraid this is a unclear-what-you-are-asking question, but if it is that obvious that there wont be any temporary foo
, that would be answer enough ;).
In C++11, can I be certain that there wont be a temporary foo
in make_foo
that will be destroyed? The destructor should be called only when x
goes out of scope.
As correctly pointed out in a comment, this question is the Y part of a XY question and the X part is that I want to implement some end of scope functionality. The destructor of foo
has some side effects (in the example the cout
) that should be called at the end of scope of x
but not in make_foo
in case there would be some temporary foo
.