I am struggling with miniproject, where I have to do this
const Foo &f = func(arr(1.3, 2.4, 3.8, 4.3));
Foo
is non-duplicable class containing single float
value as memeber, arr
is simple function that returns std::vector
of Foo
objects initialized with values passed as parameter into arr
.
Finally func
is defined as following:
const Foo &func(const std::vector<Foo> &in)
{
return *std::max_element(in.begin(), in.end());
}
Foo
has defined operator<
function, that determines which of the two Foo
s have bigger value, thus std::max_element
works as should. Foo
has also defined Foo(Foo &&v)
that copies value.
I know that parameter of func
is created as temporary.
Application is compilable, but after debugging, the object f
contains some random value, that is not even in passed array (since the object was already destroyed?).
Is there any possible way to change func
so this will work?