5.2.16 · HinglishC++ Programming

Variadic templates — parameter packs, fold expressions

1,796 words8 min readRead in English

5.2.16 · Coding › C++ Programming


1. Parameter packs — YE HAIN KYA

Teen jagah ... aata hai (KAISE padhen):

Position Matlab
typename... Ts ek type pack declare karo
Ts... args ek value pack declare karo
args... (baad mein) pack ko expand karo

2. Recursive expansion — pre-C++17 ka tarika (DERIVE karo)

Fold expressions se pehle, aap ek pack ko head ko alag karke process karte the, tail pe recurse karte hue:

// Base case: koi arguments nahi bache
void print() { }
 
// Recursive case: 'first' ko alag karo, 'rest' pack pe recurse karo
template<typename T, typename... Rest>
void print(T first, Rest... rest) {
    std::cout << first << ' ';
    print(rest...);          // expand: baaki pack pe print call karo
}

Base case kyun zaroori hai: recursion har call mein pack ko ek se chhhota karta hai. No-arg overload ke bina, aakhri print() call compile nahi hoti — recursion rokne ke liye kuch nahi hota.


3. Fold expressions — C++17 ka tarika (80/20 win)

Recursion verbose hai (ek idea ke liye do functions). Fold expressions ek pack ko ek binary operator se ek line mein collapse kar dete hain.

template<typename... Ts>
auto sum(Ts... xs) {
    return (xs + ...);        // unary right fold: x1 + (x2 + (x3 + ...))
}

sum(1,2,3) ka expansion DERIVE karo: Kyun? Unary right fold right se nest karta hai. + associative hai to value 6 hi rahegi.

Figure — Variadic templates — parameter packs, fold expressions

4. Worked examples


5. Common mistakes


Flashcards

typename... Ts kya declare karta hai?
Ek template parameter pack — zero ya zyada types jinka naam Ts hai.
Pack Ts mein elements ki number kaise nikaalte hain?
sizeof...(Ts) (ek constexpr size_t).
... ka matlab "pack declare karna" vs "pack expand karna" kahan hota hai?
Naam se pehle = declare; pattern ke baad = expand.
xs = 1,2,3 ke liye (xs + ...) expand karo.
1 + (2 + 3) = 6 (unary right fold, right se nest karta hai).
xs = 1,2,3 ke liye (... - xs) expand karo.
(1 - 2) - 3 = -4 (unary left fold, left se nest karta hai).
Kaun se 3 operators empty pack pe unary fold allow karte hain, aur unki identities?
&&→true, ||→false, ,→void().
Recursive pack processing mein base-case overload kyun chahiye?
Pack empty hone par recursion rokne ke liye.
Ek pack jo empty ho sakta hai, usse safely sum kaise karein?
Binary fold with init: (0 + ... + xs).
Pack args ke har element pe f call karne ka syntax kya hai?
(f(args), ...); ya kisi call ke andar f(args)....
Fold expression mein parentheses kyun zaroori hain?
Fold ko ... ke doosre uses se alag karne ke liye; yeh syntax rule hai.

Recall Feynman: ek 12-saal ke bacche ko samjhao

Ek magic gift-wrapping machine imagine karo. Aapko pehle se nahi pata ki aap use 1 toy doge ya 10, lekin wo sab ko wrap kar leti hai chahe kitni bhi hon. Ek parameter pack "toys ka dhera" hai, aur ... aapka yeh kehna hai "dheri mein har toy ke liye yahi karo". Ek fold toys ko stack karne jaisa hai: (x + ...) matlab "sabko ek dabbe mein daalo", right se shuru karo aur unhe milate jao jab tak sirf ek daabba (answer) na bache. Computer program chalane se pehle hi exact wrapping steps bana leta hai — to yeh flexible bhi hai AUR safe bhi.

Connections

  • Templates and Generics — variadic templates Function Templates ka general case hain.
  • std::tuple — parameter packs par bana hai; std::make_tuple variadic hai.
  • Perfect Forwardingtemplate<typename... Ts> f(Ts&&... a){ g(std::forward<Ts>(a)...); }.
  • Recursion — pre-C++17 pack processing type list pe structural recursion hai.
  • constexpr and Compile-time Computationsizeof... aur folds compile time pe evaluate hote hain.
  • Operator Overloading — folds kisi bhi binary operator ke saath kaam karte hain, overloaded wale bhi.

Concept Map

motivates

type-safe alt to

built on

declared with

before name

after pattern

counted by

processed pre-C++17

needs

processed C++17

collapses with

replaces

Unknown arg count

Variadic templates

printf unsafe

Parameter packs

Ellipsis dots

Declare pack

Expand pack

sizeof... count

Recursive expansion

Base case stops recursion

Fold expressions

Binary operator