isocpp / isocpp/CppCoreGuidelines
Additions to T.144: Don’t specialize function templates
Open
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
T.144: Don’t specialize function templates
Example
template<typename T>
string stringify(const T& x) { to_string(x); }
template<> // bad, could be overload instead,
// primary template forces us to pass string_view by const&
string stringify(const string_view& x) { return string(x); }
// better, non-template overload with improved signature
string stringify(string_view x) { return string(x); }
// alternative: "specialize" inside a single function using constexpr if-statement
template<typename T>
string stringify2(const T& x) {
if constexpr (is_convertible_v<const T, string_view>) { return string(x); }
else { return to_string(x); }
}
Alternatives
if constexprto provide alternative implementations within a function
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 existing T.144 guideline and review the supplied examples and Alternatives section for consistency with its current wording. Done means incorporating the requested guidance on overloads and constexpr if without changing the rule's intent; no file or test is named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100