5.2.15 · D3 · HinglishC++ Programming

Worked examplesTemplate specialization — full and partial

4,928 words22 min read↑ Read in English

5.2.15 · D3 · Coding › C++ Programming › Template specialization — full and partial

Yeh page assume karta hai ki tum basic template syntax se pehle mil chuke ho. Agar template<class T> abhi bhi alien lagta hai, pehle woh padho.


Pehle do words: "pattern" aur "shape gate"

Kisi bhi example se pehle, do words ko pakka karte hain jo hum baar baar use karte hain.

Neeche ki figure darwaze ka metaphor literally draw karti hai — ise dimag mein rakho, kyunki is page pe har example bas yahi hai: "kaunse gates se X guzrta hai?"

Figure — Template specialization — full and partial

Teen darwaze dekho. Har ek ek pattern ki shape mein kata hua hai: sabse wide opening T hai (primary — koi bhi type fit hoti hai), medium wali T* hai (sirf pointer-shaped types fit hoti hain), aur sabse narrow int* hai (sirf woh exact type fit hoti hai). Do travellers dekho: yellow int block pointer-shaped nahi hai, toh woh sirf sabse wide T door se guzar sakta hai. Pink int* block pointer-shaped hai, toh woh teeno doors se fit hota hai — aur compiler hamesha ise sabse narrow door se bhejta hai jisme woh fit hota hai, jo hai int*. Yahi "narrowest door wins" hai — ek picture mein poora selection rule.

Do words — pattern aur shape gate — yaad rakho: neeche ka har example bas yahi hai: "kaunse patterns' gates se X guzrta hai, aur kaunsa survivor sabse specialized hai?"


The scenario matrix

Har specialization question aslmein yahi hai: "type X ke liye kaunsa candidate jitega?" Candidates hain: primary (generic fallback), ek full spec (saare parameters concrete types pe pin), aur ek ya zyada partial specs (shape constrained, kuch parameters abhi free). Tum jo type X feed karte ho woh input hai; winning candidate output hai.

Neeche ka map har case class ko shape gates ke landscape ki tarah lay out karta hai, na ki text ki wall ki tarah. Ise ek decision fan ki tarah padho: ek input X left se enter karta hai aur exactly ek cell mein land karta hai depending on kitne gates woh fit karta hai aur woh gates kaisa relate karte hain.

Figure — Template specialization — full and partial

Figure mein har labelled node matrix ki ek cell hai — ek alag scenario jo topic tumpe throw kar sakta hai. Color outcome batata hai (white = primary fires, blue = partial fires, yellow = full spec fires, pink = error). Har node ke neeche chota tag (Ex 1, Ex 2, …) us worked example ki taraf point karta hai jo use drill karta hai. Cells hain:

  • Akoi spec match nahiprimary fires (fallback). (Ex 1)
  • B — ek full spec match karta hai → woh sab kuch beat karta hai, kyunki X exactly woh pinned type hai. (Ex 1, Ex 8)
  • C — ek partial match karta hai aur primary bhi match karti hai → partial jitati hai. (Ex 2)
  • Dek parameter pinned, doosra abhi free (mixed concrete + free). (Ex 3)
  • Edo candidates compete karte hain, ek strictly zyada specialized hai → woh subset ordering se jitata hai. (Ex 4)
  • Fdo partials tie karte hain, na to ek doosre ka subset hai → ambiguity error (the degenerate case). (Ex 5)
  • Gnested shape (T**, pointer-to-pointer) → most-nested pattern jitata hai. (Ex 6)
  • Hzero / degenerate input jaise void aur void* (ek boundary type). (Ex 7)
  • I — ek real-world word problem: ek per-type serializer. (Ex 8)
  • Jexam twist: ek function ko partially specialize karne ki koshish → uske bajaye overloading use karo. (Ex 9)
  • Klimiting behaviour: reference vs pointer vs array shapes sab alag hain, har ek apna partial fire karta hai. (Ex 10)

Neeche har worked example us cell(s) ke saath tagged hai jo woh cover karta hai. Milke woh poora map fill karte hain.


Ek picture jo tumhare dimag mein rehni chahiye

Figure — Template specialization — full and partial

Figure ko top se bottom padhdo: ek type X drop hoti hai (top, pale-yellow box), compiler saare candidates collect karta hai (white box), un sab ko bahar phenkta hai jinke pattern shape gate fail karte hain X ke liye (pink box — "unhe discard karo jinke pattern X se match nahi karte"), phir survivors mein se most specialized choose karta hai (blue box). Neeche ke teen outcome boxes — primary / partial / full — wohi teen colors hain jo tum har example mein dekhoge: fallback ke liye white, partial ke liye blue, full spec ke liye yellow.


Worked Examples



Recall

Recall

int** ke liye, rank karo ki T, T*, T** mein se kaun jitega aur kyun Teeno match karte hain; T** strict subset hai T* ka, jo strict subset hai T ka. ::: T** jitata hai — woh most specialized hai (deepest nesting = strongest constraint).

Recall Kya

V<void*> T* partial fire karta hai? Kyun ya kyun nahi? Specialization type structure se match karta hai, usability se nahi. ::: Haan — void* ka pointer shape hai (T* with T=void), toh partial fire hota hai chahe tum void* dereference nahi kar sakte.

Recall

Tag<const char*> ko "ptr:cstr" nahi balki "cstr" kyun tag kiya jaata hai? Kyunki full specialization ek matching partial ko outrank karti hai. ::: Full spec Tag<const char*> partial Tag<T*> ko beat karta hai, toh woh kabhi pointer branch mein recurse nahi karta.

Recall Ex 5 mein ambiguity error aata hai. Ise fix karne wali ek single line kya hai?

Dono partials se strictly more specialized kuch add karo. ::: template<> struct M<int,int>{}; — ek full spec dono partials ko beat karta hai, tie todta hai.

Recall

int[4] aur int& ke liye array aur reference partials ke beech kabhi ambiguity kyun nahi hoti? Unke shape gates disjoint hain. ::: Reference array nahi hai aur array reference nahi hai, toh har input exactly ek gate kholta hai — sirf ek partial kabhi survive karta hai.

Related deep tools worth a visit: SFINAE and enable_if, constexpr if, aur container quirk std::vector<bool> special case.