isocpp / isocpp/CppCoreGuidelines
T.103 should be removed or limited to immutable argument lists
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, someint, somedouble - we have a pack of strings, and some of the arguments are
char[], some arestd::string, some arestd::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 rvaluesT(&)[N]uses C-style arrays and violates other guidelinesstd::array<T, N>is really awkward to construct as a temporary argument (either with CTAD orstd::to_array)- there is a proposal for
std::make_arraywhich makes this simpler, but it would be extremely hypocritical to advice its use, given that it violates T.103
- there is a proposal for
std::span<T>cannot be constructed from prvalues ifTis mutable; it also doesn't imply ownership, so we typically shouldn'tstd::moveits 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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