The title says it all.
The first case f(const X&) is the good old fashioned const reference parameter.
However, when taking a mutable reference parameter, why not always use X&&? This way client code can use temps or regular lvalues, rather than f() dictating usage.
EDIT: Sloppiness on my part, sorry. I meant, given fc(const X&) and fm(X&&), do we need fm(X&)?
EDIT: Example: I use stateless decorators to pass as parameters:
class Base64Writer: public Writer {
public:
Base64Writer(Writer& w): w_(w) {}
private:
virtual void doWrite() override;
Writer& w_;
}
void func(Writer&& w) { w.doWrite(); }
// to be called as
Writer wr = ...;
func(Base64Writer(wr));
If you mean to ask : is it logical to have all three overloads at the same time?
In most cases, it would be a bad idea. as the semantic of the overloaded functions contradict each other. One promises that it will not modify the argument, the other doesn’t make any such promise (it tacitly says it will modify the argument!). The user of such overloaded functions will be confused that differs only by
const.It makes sense to have these overloads at the same time:
But a third one, in addition to the above two, doesn’t make much sense to me:
It only adds confusion and makes code harder to read. If you want this semantic, choose a different name for the function!
If you mean to ask : which one should you have (though not necessarily all at the same time)?
I would say, it depends on the semantic. If the parameter behaves as input and/or output parameter, then
f(X&)makes sense. If the parameter is only input parameter, thenf(X const&)makes sense, in this case you can additionally definef(X&&)also, to gain performance advantage!