isocpp / isocpp/CppCoreGuidelines

T.103 should be removed or limited to immutable argument lists

Open
#2,086 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

T.103: Don't use variadic templates for homogeneous argument lists
Reason

There are more precise ways of specifying a homogeneous sequence, such as an initializer_list.

T.103 is not very useful for truly homogeneous argument lists

If you are working with a truly homogeneous argument list, such as obtaining all of your arguments from a std::array of one type, then you should be using algorithms based on ranges and iterators instead of variadic functions.
std::initializer_list doesn't solve any issues in this case, it can only make things worse by preventing move semantics.

T.103 is too restrictive for mostly homogeneous argument lists

Otherwise, the argument list is typically heterogeneous in some way. Some examples:

  • we have a numeric pack, and some of the arguments are float, some int, some double
  • we have a pack of strings, and some of the arguments are char[], some are std::string, some are std::string_view
  • we have a pack of std::strings, but some are rvalues, and some are lvalues

In these "mostly homogeneous" cases, the language is deficient and provides no true replacement for variadics:

  • std::initializer_list<T> cannot be moved from, so it wastes the potential of any rvalues
  • T(&)[N] uses C-style arrays and violates other guidelines
  • std::array<T, N> is really awkward to construct as a temporary argument (either with CTAD or std::to_array)
    • there is a proposal for std::make_array which makes this simpler, but it would be extremely hypocritical to advice its use, given that it violates T.103
  • std::span<T> cannot be constructed from prvalues if T is mutable; it also doesn't imply ownership, so we typically shouldn't std::move its contents, leading to potential needless copies

In addition, all of these alternatives are uglier at the call site, because they require an extra pair of braces at the very least. They don't allow for perfect forwarding and needlessly invoke the move constructor once, at least.

Suggested Fix

T.103 should be removed completely or limited to immutable homogeneous argument lists.
When the arguments are all used in a non-mutating way, it's possible to use:

  • std::initializer_list<T>
  • std::span<const T>
  • const std::array<T, N>

... without great cost.

-### T.103: Don't use variadic templates for homogeneous argument lists
+### T.103: Don't use variadic templates for immutable homogeneous argument lists

##### Reason
There are more precise ways of specifying a homogeneous sequence, such as an `initializer_list`.

##### Example
-???
+    int sum(initializer_list<int> args)
+    {
+        return reduce(begin(args), end(args));
+    }
+
+    string concatenate(initializer_list<string_view> args)
+    {
+        return accumulate(begin(args), end(args), string{});
+    }
+
+    int s = sum({1, 2, 3, 4}); // 10
+    string c = concatenate({"prefix: ", get_c_string(), "\n"}); // prefix: ...\n

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the T.103 guideline text and the proposed wording and examples in this issue. Compare the cases for truly homogeneous, mostly homogeneous, and immutable argument lists, then determine whether T.103 should be removed or narrowed; done means a maintainer-approved rule and updated rationale and examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.