5.2.15 · D1 · HinglishC++ Programming

FoundationsTemplate specialization — full and partial

2,010 words9 min read↑ Read in English

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

Isse pehle ki tum template<class T> struct Box<T*> padh sako aur jaano ki iska matlab kya hai, tumhe har piece padhna aana chahiye: template kya karta hai, angle brackets <...> mein kya hota hai, class T versus <T*> versus <> mein se har ek kya kehta hai. Yeh page inhe sab zero se build karta hai, us order mein jisme yeh stack hote hain.


1 — Ek type kya hoti hai?

Picture yeh hai: memory ko boxes ki ek lambi strip samjho. Ek type ek stencil hai jo strip par rakh di jaati hai aur kehti hai "yeh 4 boxes milke ek int hain; inhe is tarah padho."

Figure — Template specialization — full and partial

Yeh topic ko kyun chahiye: specialization poori tarah is baat ke baare mein hai ki tum kis type ke liye override kar rahe ho. Agar tum int, int*, aur pair<A,B> ko clearly alag-alag shapes wali alag types nahi dekhte, toh poora page dhundh jaisa lagega.


2 — Angle brackets <...> aur "generic" ka matlab

Do lines compare karo jo tum pehle se thoda jaante ho:

  • max(3, 5) — round brackets values 3 aur 5 pass karte hain.
  • Box<int> — angle brackets ek type int pass karte hain.

Yeh topic ko kyun chahiye: full specialization template<> likhti hai (koi blank nahi bache) aur partial likhti hai template<class T> ... <T*> (ek blank abhi bhi khula hai, par shape mein constrained hai). Tum un dono mein fark tab tak nahi jaanoge jab tak "blank" (parameter) aur "filled-in value" (argument) tumhare dimaag mein alag ideas na hon.


3 — template<class T> : recipe declare karna

Picture yeh hai: template<class T> ek cookie-cutter machine hai. Use int do → woh int version stamp out karta hai. Use double do → double version. Ek machine, unlimited cookies.

Figure — Template specialization — full and partial

Yeh topic ko kyun chahiye: parent mein primary template (template<class T> struct TypeName) exactly yahi machine hai. Har specialization iske relative define hoti hai.


4 — Instantiation: jab recipe real code ban jaati hai

Picture yeh hai: recipe ek cookbook mein bekar baithti hai. Pehli baar jab tum Box<int> order karte ho, kitchen actually use pakati hai. Woh "pehli baar pakana" instantiation hai.

Yeh topic ko kyun chahiye: parent page par poora "selection rule" instantiation time par fire karta hai — tab hi compiler decide karta hai ki konsi recipe (primary / full / partial) run karni hai.


5 — Pointer star * aur type ki "shape"

Figure — Template specialization — full and partial

Yeh topic ko kyun chahiye: partial specialization = shape par matching, exact identity par nahi. Agar int* ka mental image nahi hai ki yeh "int ke around ek wrapper" hai, toh pattern TypeName<T*> ka koi matlab nahi.


6 — struct { ... }, static, aur ::

Picture yeh hai: Box<int> ek labelled drawer hai. static members drawer ke front par printed hain, bina khole readable. :: tumhari ungli hai jo us label ki taraf point kar rahi hai.

Yeh topic ko kyun chahiye: har worked example Printer<bool>::fmt() ya IsPtr<int*>::value call karta hai. :: se hi tum specialized recipe se poochhte ho ki usne kya produce kiya.


7 — template<> : "koi blank nahi bache" ka marker

Ise blanks ke countdown ki tarah padho:

Tum kya likhte ho Baaki blanks Matlab
template<class T> struct Box 1 (T) primary recipe
template<class T> struct Box<T*> 1 (T), par shape pointer tak lock partial spec
template<> struct Box<int> 0 exactly int ke liye full spec

Yeh topic ko kyun chahiye: yeh table parent se "teen cheezein distinguish karna" hai, par ab usmein har symbol earn kiya hua hai. template<> bhool jaana (parent trap #1) compiler ko lagata hai ki tum primary redeclare kar rahe ho — jo ab sense karta hai, kyunki template<> marker ke bina koi signal nahi hai "override aa raha hai."


8 — "More specialized" = matching types ka chhota set

Figure — Template specialization — full and partial

Yeh topic ko kyun chahiye: parent ka "partial ordering" aur ambiguity error pure set-logic hai. Do patterns tab ambiguous hote hain jab dono circles ek doosre ke andar nahi baithe — woh bina nesting ke overlap karte hain. Ab woh word "ambiguous" yaad karne ki rule nahi, ek picture hai.


Prerequisite map

Type: int double bool

Angle brackets and arguments

Pointer star and shape

template of class T recipe

Instantiation at first use

Partial spec matches shape

template empty means full spec

Selection rule at instantiate time

Sets of types and subset

Template Specialization full and partial

Parent par action mein dekho: Template specialization — full and partial (index 5.2.15). Agle natural stops hain Templates — function and class basics (recipe machine itself), Type Traits and std::is_pointer (partial spec ek tool ki tarah), aur Overload Resolution vs Specialization (kyun functions ko partially specialize nahi kiya ja sakta).


Equipment checklist

Ek type compiler ko do cheezein batati hai — kaun si?
Value kitni badi hai (bytes) aur usmein kaunse operations allowed hain.
Template parameter aur template argument mein fark?
Parameter = recipe mein naam diya hua blank (T); argument = daali gayi concrete type (int).
Kya template<class T> mein class T ko class hone par force karta hai?
Nahi — iska matlab sirf "type parameter" hai; typename T identical hai aur T int, pointer, kuch bhi ho sakta hai.
Instantiation kya hai aur kab hoti hai?
Compiler ka recipe ko ek specific type ke liye real code mein stamp karna, type ke first use par trigger hota hai.
Type int* ka kya matlab hai, aur yeh "shape" kaise hai?
"Woh address jahan ek int rehta hai" — int ke around ek pointer wrapper; shape hai "pointer-to-something".
Printer<bool>::fmt() mein :: scope operator kya karta hai?
Type Printer<bool> ke andar jaake uska member fmt utha laata hai.
template<> (khaali brackets) kya signal karta hai?
Zero blanks baaki hain — yeh exact types ke liye FULL specialization hai.
Set terms mein, "more specialized" ka kya matlab hai?
Pattern types ka CHHOTA set claim karta hai (ek subset), isliye compiler use prefer karta hai.
Do partial specializations kab ambiguous hoti hain, sets ki picture mein?
Jab unke type-sets overlap karte hain par dono mein se koi doosre ka subset nahi — koi nesting nahi.
Specialization type ke first use se pehle kyun aani chahiye?
Kyunki generic recipe first instantiation par pak jaati hai; baad mein aaya override bahut der se aata hai.