Proposal: explicitly mark out-parameters from F.23 with output<T>
We now have two guidelines:
F.22: Use a T& for an in-out-parameter
and
F.23: Use T& for an out-parameter that is expensive to move (only)
that both suggest using T&.
It might be beneficial to explicitly differentiate between in-out-parameters and out-parameters. If we introduce
template <class T>
using output = T&;
to explicitly mark output-only parametrs we'll get 2 benefits: 1. it will be clear whether any given parameter is in-out or out-only (right now it can be understood only by analysing the function's source code) 2. static analyzer could flag methods where not all code pathes result in modifying all out-only parameters.
It might also be possible to flag cases when a const method of the out-only parameter is called before any non-const method of this parametr is called (which is most probably a bug).
For example:
void Method(int inputParameter, BigData& inOutParameters, output<ClassExpensiveToMove> outParameter);
I agree that this makes code more explicit and easier to write. Thought, out parameters should be rare, right? I think... hehe.
C-style API at ABI-stable boundaries have to use pointers for output parameters.
We recommend return values for pure output parameters, with the necessary exception in F.23. I suspect that output<> would have to be an alias for static analysis, like owner<>. It would be hard to name. For now, I will leave this issue open, pending more experience. gdr-at-ms's comment also apply.
Editors discussed on 11/16/2015.
This should now be addressed as part of this week's merger of those and all the other parameter passing items. The result is now in the newly-renumbered F.15-21.
PS -- To be clearer, I should add that the cases where you would use a & parameter instead of a return value for an "out" parameter are now (correctly I think) characterized as "what you're really saying is you're treating it as in-out."
Editors discussed on 12/8/2015
Discussing writeonly with a colleague, it feels somewhat like the opposite of std::move. It seems like a reasonable expectation that I can pass a reference to a moved-from T into a writeonly<T&>, particularly given C.64 strengthening the state of moved-from from "destructible" to "assignable-to or better".
The way I'm reading the H1-branch draft, I expect that a method that gets a writeonly<vector<T>> buffer is going to have buffer.clear(); somewhere. If the method is intending to append to that vector, it's really an in-out parameter and should be vector<T>&. Is that how it's intended?
If that's right, then it seems the static analysis activity of a use of a writeonly<T> value, is going to be similar to that of the use of a moved-from value.
There is no consensus among the editors for now, but we may look at this again in the future. See also #443.