TRT
Sign in

Advanced · Module 9 · Lesson 3 of 7

Specialization: Customizing for One Type

Keep one generic print for all types, specialize it for string literals and std::string, and fix the missing include that blocks compilation.

Pro — members onlyAbout 9 minutes

By the end you can

  • Write full specialization for const char* and std::string print
  • Explain why C strings need content comparison, not address order
  • Predict overload choice between generic and specialized print

Builds on Class Templates: A Generic Pair and Container.

01 · A question first

Generic print handles ints and doubles cleanly, but string literals print as pointers and std::string copies on every call. How do you keep the generic version for most types while giving two string forms their own behavior, and which include makes the string overload compile at all?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • A second runnable example
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem