A [[byvalue]] attribute for C++?

Background C++ attributes are a useful way to document assumptions and intent within code, and to prevent warnings. (e.g. [[fallthrough]]) Now, the Core Guidelines recommend to For “in” parameters, pass ... by reference to const That means that functions taking e.g. an input std::string will normally look like this: void take_s(std::string const& s) { // or `const std::string&`, no matter here ... However, it is not quite uncommon to have small functions that need to mutate a copy of their argument before proceeding. For these cases I use the following pattern, to highlight the intent: void take_s(std::string /*byvalue*/ s) { mutate(s); ... I have also encountered this in ranged based for loops: Normally: for (const auto& s : str_list) ... Sometimes: for (const /*copy*/ auto s : str_list) ... Question It therefore occured to me that a C++ Attribute [[byvalue]] (instead of the comments shown above) would be useful here to document the intent -- and possibly also support better warnings just as with other attributes. However I have been unable to find any prior discussion wrt. this: Given that attributes have been around for a while, this leads me to the assumption that my idea does not really fit C++ attributes or that I'm missing something crucial. Would such a [[byvalue]] attribute somehow conflict with the intended use cases of C++ attributes?

May 14, 2025 - 15:26
 0

Background

C++ attributes are a useful way to document assumptions and intent within code, and to prevent warnings. (e.g. [[fallthrough]])

Now, the Core Guidelines recommend to For “in” parameters, pass ... by reference to const

That means that functions taking e.g. an input std::string will normally look like this:

void take_s(std::string const& s) { // or `const std::string&`, no matter here
...

However, it is not quite uncommon to have small functions that need to mutate a copy of their argument before proceeding. For these cases I use the following pattern, to highlight the intent:

void take_s(std::string /*byvalue*/ s) {
  mutate(s);
  ...

I have also encountered this in ranged based for loops:

Normally:

for (const auto& s : str_list) ...

Sometimes:

for (const /*copy*/ auto s : str_list) ...

Question

It therefore occured to me that a C++ Attribute [[byvalue]] (instead of the comments shown above) would be useful here to document the intent -- and possibly also support better warnings just as with other attributes.

However I have been unable to find any prior discussion wrt. this:

Given that attributes have been around for a while, this leads me to the assumption that my idea does not really fit C++ attributes or that I'm missing something crucial.

Would such a [[byvalue]] attribute somehow conflict with the intended use cases of C++ attributes?