5.2.16 · D1 · HinglishC++ Programming

FoundationsVariadic templates — parameter packs, fold expressions

2,242 words10 min read↑ Read in English

5.2.16 · D1 · Coding › C++ Programming › Variadic templates — parameter packs, fold expressions

Parent note ki koi bhi line padhne se pehle, tumhe har woh symbol khud se samajhna hoga jo woh throw karta hai. Is page mein woh sab build-order mein listed hain: har naya symbol sirf wohi use karta hai jo uske upar already define ho chuka hai. Kuch bhi assume nahi kiya gaya.


0. Sabse pehli picture: "dher" kaisa dikhta hai

Jab tum ek ordinary function call karte ho toh tum use fixed number of values dete ho:

add(3, 4)   // exactly two things

Ek variadic function instead values ka ek dher accept karta hai — 0, 1, 2, 5, sau — aur hum us dher ko ek row mein rakhe boxes ki tarah draw karte hain.

Figure dekho. Blue boxes woh individual values hain jo tumne pass kiye. Neecha wala yellow bracket, jis par args likha hai, ek saath poore dher ka naam hai. Woh ek yellow naam hi woh cheez hai jise is topic ka baaki hissa manipulate karta hai. Yeh picture apne dimaag mein rakho: ek naam, kaafi saare boxes.


1. template — YEH PEHLE KYUN AATA HAI

Variadics se pehle, tumhe template ka idea chahiye.

template<typename T>   // T is the blank
T bigger(T a, T b) { return a > b ? a : b; }

Topic ko yeh kyun chahiye: ek parameter pack ek template blank hi hai jo ek ki jagah kaafi saare types hold karta hai. "Pack" ko samjhe bina "ek blank" samajhna zaroori hai. Pure stamp ki kahani ke liye Templates and Generics aur Function Templates dekho.

Pieces ke liye reading guide:

  • template<...> — "aage blanks wali recipe aa rahi hai".
  • typename T — "blank ek type hai, aur main use T kahunga".
  • T a, T b — ordinary parameters jo uss blank ko use karte hain.

2. typename — BLANK MEIN KYA HOGA

Hume kyun parwah hai: template<typename... Ts> mein (parent ka bilkul pehla formula) typename word tumhe batata hai ki pile Ts types ka dher hai, values ka nahi. Do alag-alag dher aayenge (types aur values) aur yeh keyword type wale ko mark karta hai.


3. Ellipsis ... — sabse important symbol

Is topic mein jo bhi mushkil hai woh ek symbol hai jo do directions mein use hota hai. Hum ise ek figure ke saath dheere-dheere lete hain.

Figure ke dono halves study karo.

Left half (declare): dots naam Ts ke left mein hain. Yeh machine ka intake hai — yeh kaafi alag-alag cheezein Ts naami ek dher mein scoop karta hai. Isliye parent typename... Ts likhta hai.

Right half (expand): dots ek chote se pattern ke right mein hain. Yahan machine dher ko unpack karta hai, pattern ko har box ke liye ek baar repeat karta hai. Toh args... ban jaata hai a0, a1, a2, aur pattern f(args)... ban jaata hai f(a0), f(a1), f(a2) — dots ke left wali poori shape har box ke liye copy hoti hai.

Topic ko yeh kyun chahiye: ... ke bina tumhe har box ko haath se naam dena padta (a0, a1, ...), lekin tumhe pata nahi kitne boxes hain. Ellipsis woh "har box ke liye" instruction hai jo ek unknown-length dher ko ek line mein likhne laayak banata hai.


4. Doh prakar ke pack — types vs values

Parent do pile use karta hai jo lagbhag identical dikhte hain. Inhe abhi alag karo.

Yeh ek matched pair ki tarah aate hain: args ki har value ka Ts mein ek type hota hai, box for box.

template<typename... Ts>   // Ts  = pile of TYPES
void f(Ts... args);        // args = pile of VALUES (one per type)

5. sizeof... — KITNE boxes hain (bytes kitne hain nahi)

Tum sizeof... ke saath do words miloge:

Toh sizeof...(Ts) hai "ek compile-time count, type size_t ka". Isse zyada mysterious kuch nahi.


6. Binary operators aur association — folds ke neeche ki zameen

Ek fold expression (parent ka Section 3) ek binary operator use karke dher ko collapse karta hai. Pehle tumhe do words pata hone chahiye.

Ab woh subtle wala — kaunsa pair pehle jaata hai jab tum inhe chain karte ho.

Figure same teen numbers 1, 2, 3 ko do tarike se fold kiya dikhata hai.

  • Left fold (top, blue): sabse pehle leftmost pair squeeze karo → (1-2)-3 = -4.
  • Right fold (bottom, yellow): sabse pehle rightmost pair squeeze karo → 1-(2-3) = 2.

Same numbers, same operator, alag answer — kyunki - ko grouping se fark padta hai. Isliye hi parent mein chaar folds hain (left/right × unary/binary): dots jis taraf hain woh batata hai kaunsa pairing pehle hota hai.

Parent se bridge: ek fold mein, ... us taraf hota hai jo "sabse gehri jagah khulta hai". Dots left pe → left-associative. Dots right pe → right-associative. Ek hi rule charon folds decode karta hai.


7. Recursion — folds se pehle ka purana engine

Parent ka Section 2 folds ke bina, recursion use karke pile process karta hai.

Yeh matter karta hai kyunki yeh explain karta hai fold expressions kyun banaye gaye: recursion ek idea express karne ke liye do functions chahta hai (chhilne wala aur khaali-pile roknewala), aur folds yeh kaam ek hi line mein karte hain.


8. Do aur names jo tumhe milenge

Parent padhne ke liye inhe master karna zaroori nahi — bas names pehchaanlo.


Prerequisite map

templates - code with blanks

typename - the blank is a type

ellipsis - do this for every box

type pack Ts

value pack args

sizeof... - how many boxes

binary operators

association - which pair first

fold expressions

recursion + base case

old-style pack processing

Variadic Templates

Ise upar se neeche padho: templates tumhe ek blank dete hain, typename kehta hai blank ek type hai, ellipsis ek blank ko kaafi saari cheezein hold karne deta hai, aur do branches (folds vs recursion) pile use karne ke do tarike hain.


Equipment checklist

Right side cover karo aur zor se jawab do. Agar koi ruk jaaye, uska section upar dobara padho.

Ek phrase mein template
Ek recipe for code with a type-blank jise compiler fill karta hai.
typename kis cheez ke liye jagah reserve karta hai
Ek type ke naam ke liye (int, string, ...), value ke liye nahi.
... ke do kaam aur unhe kaise pehchanein
Naam se pehle = pack declare karo; pattern ke baad = use expand karo. Dekho dots kis taraf hain.
Pack Ts aur pack args mein fark
Ts types ka dher hai; args matching values ka dher hai.
sizeof...(Ts) kya return karta hai
Elements ki count — ek compile-time size_t, byte size NAHI.
sizeof...sizeof kyun
sizeof = ek object ke bytes; sizeof... = pack elements ki sankhya.
a - b - c ke liye "left-associative" ka matlab
Left se group karo: (a - b) - c.
Fold direction - ke liye matter karta hai lekin + ke liye nahi kyun
- alag grouping mein alag answers deta hai; + order-independent hai.
Recursion mein base case kya karta hai
Jab pile khaali ho toh self-calls rokta hai, taaki recursion terminate ho.
Kaafi tasks ke liye fold expressions ne recursion kyun replace kiya
Recursion ko ek idea ke liye do functions chahiye; fold yeh ek hi line mein karta hai.