So I have the following function:
void scan(std::istream& is, Handler& h);
I want to call it in different ways, like:
scan(std::cin, Handler());
scan(std::ifstream("myfile"), myhandler);
The compiler complains about std::ifstream("myfile")
and Handler()
of being rvalues being passed as non-const references, so the complaint is legitimate, but what can I do?
- Neither function parameters cannot be
const
(istream
is modified while read and the handler changes its state during callbacks). - If I change the parameter types to rvalue references (
&&
) then I will not be able to passstd::cin
and sometimes I really care about the final state ofmyhandler
thus I cannot applystd::move
on them neither. - In principle I could make the parameters as universal references via template or
auto&&
type deduction and thus overload this function for all possible combinations of lvalue and rvalue references, but I have no intention of overloading this function for other types than I have already specified.
Are there any other options?
Somehow this whole move semantics got in the way in such a trivial example.