isocpp / isocpp/CppCoreGuidelines
T.101 Proposal for how to pass arguments to a variadic template
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
I have a suggestion to fill in the missing known information for T.101. The standard library passes parameter packs by perfect forwarding in almost all cases, and that should be the norm. For read-only operation, it generally makes more sense to use const& though, because read-only operators virtually never have an rvalue overload, and accepting const& is more const-correct anyways.
T.101 Pass parameter packs via const reference or forwarding reference
Reason
Allow passing lvalues and rvalues, and avoid unnecessary copy/move construction.
Example
// all arguments passed as const&, because we don't mutate any arguments
template<typename L, typename... Rs>
auto equal_to_any(const L& l, const Rs&... rs) const
{
return (l == rs || ...);
}
// we can mix lvalues like zero_object and rvalues like 0
bool is_x_zero = equal_to_any(x, nullptr, 0, "", "zero", zero_object);
// args are passed as forwarding reference, because the consumer might
// need to modify them, move them, ...
template <typename Consumer, typename... Args>
void consume_all(Consumer consume, Args&&... args)
{
(consume(forward<Args>(args)), ...);
}
Enforcement
Flag parameter packs which are passed by value or lvalue-reference.
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 by reviewing the existing T.101 guideline and its surrounding parameter-pack guidance in the C++ Core Guidelines. Compare the proposed const-reference and forwarding-reference examples with current wording, then update T.101 so the recommendation and enforcement statement are unambiguous.
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
- 30/100