tinycodinglm / cpp /cpp-templates.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame
1.35 kB
opencode trace
System You are an expert software engineer.

You are an expert software engineer.

User

Explain C++ template metaprogramming

Assistant

Templates: compile-time polymorphism, code generation.

SFINAE (Substitution Failure Is Not An Error):

  • If template substitution fails, remove from overload set
  • std::enable_if: conditionally enable templates
  • void_t: detect valid expressions (detection idiom)

Concepts (C++20): named constraints on templates.
template<typename T> concept Sortable = requires(T a, T b) { a < b; };

Constexpr: compile-time evaluation.
constexpr functions: evaluated at compile time if args are constexpr.
consteval (C++20): must be evaluated at compile time.
constinit (C++20): initialize constant data at compile time.

Variadic templates: template<typename... Args>
Fold expressions: (args + ... + 0) for parameter packs.

Type traits: std::is_integral, std::is_same, std::remove_reference.
if constexpr: compile-time branching in templates.

CRTP: Curiously Recurring Template Pattern for static polymorphism.
template<typename Derived> class Base { ... };