5.2.17 · D1 · HinglishC++ Programming

FoundationsSFINAE — substitution failure is not an error

2,255 words10 min read↑ Read in English

5.2.17 · D1 · Coding › C++ Programming › SFINAE — substitution failure is not an error

Is page pe yeh assume kiya gaya hai ki aapne parent note ki koi bhi notation pehle nahi dekhi. Hum har piece build karenge — template, typename, T, ::, decltype, ..., enable_if, _v, _t — bilkul ground up se, ek aisi order mein jahan har idea sirf pehle se explain kiye gaye ideas use karta hai.


0. Stage set karna: function overload kya hota hai?

Kisi bhi template se pehle, humein overload shabd samajhna hoga.

Socho ek wall of labelled mailboxes, sab ka naam print hai, har ek alag shape ka letter accept karta hai. Aap ek letter daalo; post office decide karta hai woh kis box mein jaayega.

Figure — SFINAE — substitution failure is not an error

1. template <typename T> — ek blank wali recipe

template <typename T>   // "kisi type T ke liye ..."
T twice(T x) {          // ... yahan ek function hai jo x ko double karta hai
    return x + x;
}
  • T ek real type nahi hai. Yeh ek placeholder hai.
  • Real function tab tak exist nahi karta jab tak aap twice ko kisi concrete cheez ke saath call nahi karte.

Aapko templates isliye chahiye kyunki SFINAE ek rule hai sirf templates ke baare mein. Non-template functions mein koi blank nahi hota, toh kuch "substitute" karne ko nahi, aur kuch quietly drop karne ko nahi. Dekho Templates and Type Deduction ki T call se kaise guess kiya jaata hai.


2. Substitution aur deduction — blank fill karna

Yeh do words milte-julte lagte hain lekin alag steps hain.

Figure — SFINAE — substitution failure is not an error

3. Signature vs body — paste KAHAN dekha jaata hai

Yeh poore topic ki sabse important distinction hai.

Parent ki résumé image pe wapas jaayein: ek broken header résumé ko chupke se bin mein daal deta hai (SFINAE); theek header lekin interview ke andar bolte waqt bakwaas (body) boss ko gussa dilata hai (hard error). Signature header hai; body interview hai.


4. :: operator — type ke andar jaana

std::vector<int>::value_type   // "value_type" jo vector<int> ke andar rehta hai  →  int
int::value_type                // ??? int mein koi value_type member nahi  →  NONSENSE

Isliye parent ka pehla example (typename T::value_type) T = int ke liye fail hota hai: substitution ke baad label ek aisa drawer kholne ki koshish karta hai jo exist hi nahi karta.


5. decltype(expr) — "expression ka type poocho bina use run kiye"

Hume type likhne ki bajaye iske liye ek special tool kyun chahiye? Kyunki templates ke saath hum type nahi jaante — woh blank T par depend karta hai. Sirf compiler, substitution ke baad, jaanta hai ki t.size() kya return karta hai. decltype hume kehne deta hai "jo bhi woh nikle."

Figure — SFINAE — substitution failure is not an error

Dekho decltype and Trailing Return Types ki auto f(...) -> decltype(...) return type parameters ke baad kaise likhta hai (taaki parameter names jaise t pehle se scope mein hon test karne ke liye).


6. decltype ke andar comma operator

Parent likhta hai decltype(t.size(), size_t{}). Do cheezein comma se separated — uska kya matlab hai?

Toh decltype(t.size(), size_t{}):

  • check karta hai ki t.size() ek valid expression hai (validity gate → SFINAE),
  • lekin resulting type size_t{} ka type hai = size_t.

7. ... — variadic "catch-all" parameter


8. std::enable_if, _t, _v — switch

Ab hum pichle pieces ko modern tool mein assemble karte hain.

:: ke saath combine karo (Section 4): enable_if_t<false, R> likhna ek aisi type ka ::type drawer kholne ki koshish karta hai jisme aisa koi drawer nahi → label mein nonsense → SFINAE overload drop kar deta hai.

Figure — SFINAE — substitution failure is not an error

Dekho std::enable_if and Tag Dispatch ki yeh overloads split karne ke liye kaise use hota hai, aur Concepts (C++20) is poori machinery ke readable modern replacement ke liye. void_t Detection Idiom same "drawer appear ya vanish karao" trick use karta hai std::void_t ke saath.


9. Pieces topic ko kaise feed karte hain

overload set - many same-named functions

overload resolution - pick the best

template T - a recipe with a blank

deduction - guess T

substitution - paste T into the label

signature vs body - WHERE nonsense counts

scope operator - reach inside a type

decltype - type of an expression

comma operator - validity gate

enable_if plus _t and _v - true or false to drawer exists

ellipsis - lowest priority fallback

SFINAE - bad label means quietly drop candidate


Equipment checklist

Right side cover karo aur khud test karo. Agar koi bhi jawaab fuzzy lage, main note se pehle woh section dobara padho.

Overload set kya hota hai, aur kaunsa step uska ek member chunता hai?
Same naam share karne wale functions ki family; overload resolution aapke arguments ke liye single best match chunता hai.
template <typename T> kya create karta hai, aur T kya hai?
Ek blank wali recipe; T "kisi type jo hum abhi nahi jaante" ke liye placeholder hai.
Deduction vs substitution — pehle kaunsa hota hai aur har ek kya karta hai?
Pehle deduction (call se T guess karo), phir substitution (woh T signature mein paste karo).
SFINAE (hard error nahi) apply ho, uske liye nonsense kahan appear karni chahiye?
Signature/label mein — return type, parameter types, template-parameter defaults — body mein kabhi nahi.
A::B ka kya matlab hai, aur agar B A mein exist nahi karta toh kya hota hai?
"A ke andar rehne wala member B"; agar missing hai, toh type invalid hai — ek substitution failure.
decltype(expr) aapko kya deta hai, aur kya yeh expr run karta hai?
Woh type jo expr produce karta; yeh expression RUN NAHI karta.
decltype(t.size(), size_t{}) mein comma kya karta hai?
t.size() ko validity ke liye check karta hai (SFINAE gate) lekin overall type size_t banata hai.
(...) parameter ideal SFINAE fallback kyun hai?
Yeh lowest-priority match hai, toh sirf tab jeetta hai jab saare constrained overloads remove ho jaayein.
Suffixes _t aur _v ka kya matlab hai?
_t = "... ka ::type member"; _v = "... ka ::value (bool/number)".
enable_if_t<Cond, R> substitution failure kab ban jaata hai?
Jab Cond false hoti hai, kyunki ::type drawer exist nahi karta, aur missing member ko naam dena label mein nonsense hai.

Connections

  • ↑ Main SFINAE note par wapas
  • Templates and Type Deduction
  • Overload Resolution
  • decltype and Trailing Return Types
  • Type Traits (std::is_integral, std::void_t)
  • std::enable_if and Tag Dispatch
  • void_t Detection Idiom
  • Concepts (C++20)