From 0a7d70851cbae5aee58d97a8b24fdbdddd5fb0b0 Mon Sep 17 00:00:00 2001 From: Sergey Zubkov Date: Mon, 8 Aug 2022 15:11:21 -0400 Subject: [PATCH] F.17: note about passing reference wrappers by value (issue #1948) --- CppCoreGuidelines.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index e7d75a7..9d14c56 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -2954,6 +2954,18 @@ This makes it clear to callers that the object is assumed to be modified. ##### Note +Some user-defined and standard library types, such as `span` or the iterators +are [cheap to copy](#Rf-in) and may be passed by value, while doing so has +mutable (in-out) reference semantics: + + void increment_all(span a) + { + for (auto&& e : a) + ++e; + } + +##### Note + A `T&` argument can pass information into a function as well as out of it. Thus `T&` could be an in-out-parameter. That can in itself be a problem and a source of errors: