I know this might be a stupid question but I am not sure how to describe it properly.
When I try to call std::transform function for example,
template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform ( InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op )
one may write UnaryOperator op; and then call transform(..,..,.., op);
However I also see people write just transform(..,..,..,UnaryOperator());
My question is: is it true that classname() is equivalent to a class object?
I don’t know what exactly do you mean by
classname(), but when you write like this:… you create
opobject of classUnaryOperatorand pass it totransform. Its lifetime depends on the scope where it was defined. I.e. you can access it after the call totransform.And when you write like this:
… you essentially create a temporary object of class
UnaryOperatorand pass it totransformfunction, and its lifetime is tied to expression. Also, in this case it will be either a constant reference or an “rvalue”. Whereas in the first case it is either non-constant or constant reference.