isocpp / isocpp/CppCoreGuidelines

Additions to T.144: Don’t specialize function templates

Open
#2,105 2 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.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 constexpr to provide alternative implementations within a function

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.