CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Proposal: explicitly mark out-parameters from F.23 with output<T>

Open istolboff opened this issue 11 years ago • 9 comments

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);

istolboff avatar Oct 26 '15 07:10 istolboff

I agree that this makes code more explicit and easier to write. Thought, out parameters should be rare, right? I think... hehe.

germandiagogomez avatar Oct 26 '15 09:10 germandiagogomez

C-style API at ABI-stable boundaries have to use pointers for output parameters.

gdr-at-ms avatar Oct 26 '15 13:10 gdr-at-ms

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.

BjarneStroustrup avatar Nov 03 '15 22:11 BjarneStroustrup

Editors discussed on 11/16/2015.

AndrewPardoe avatar Nov 16 '15 20:11 AndrewPardoe

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.

hsutter avatar Dec 02 '15 19:12 hsutter

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."

hsutter avatar Dec 02 '15 19:12 hsutter

Editors discussed on 12/8/2015

AndrewPardoe avatar Dec 08 '15 22:12 AndrewPardoe

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.

TBBle avatar Jan 06 '16 12:01 TBBle

There is no consensus among the editors for now, but we may look at this again in the future. See also #443.

hsutter avatar Jan 25 '16 19:01 hsutter