Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

templates-intro

Templates intro

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

An orientation to the C++ track—how the compiled playground works, core vocabulary, and what you will practice next.

You need a clear map of the C++ track so classes, RAII, templates, and the STL do not feel like magic.

You will apply Templates intro in contexts like: Generic libraries, metaprogramming utilities, and header-only frameworks.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true). Also read the interview prep blocks; read compiler errors from the bottom up—they often point at the real type mismatch.

After the C track or equivalent—C++ builds on C memory ideas and adds OOP, templates, and the STL.

Templates let you write generic code parameterized by types or values—the mechanism behind std::vector<T> and std::sort.

Function template

template <typename T>
T maxValue(T a, T b) {
    return (a < b) ? b : a;
}

The compiler generates concrete functions for each used type—compile-time instantiation, not runtime JVM generics.

Important interview questions and answers

  1. Q: Templates vs virtual functions?
    A: Templates resolve at compile time per type (monomorphization); virtual uses runtime dispatch on inheritance.
  2. Q: Template definitions in headers?
    A: Instantiations need full definition visible—typically templates live in headers.

Self-check

  1. When is a template instantiated?
  2. Why are templates often header-only?

Tip: Template error messages are verbose—start with simple function templates before class templates.

Interview prep

Templates vs virtual?

Templates instantiate at compile time per type; virtual functions use runtime dispatch on inheritance.

Templates in headers?

Full template definitions must be visible at instantiation sites—typically in headers.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Template vs macro?
  • typename when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump