5.2.17 · D3 · HinglishC++ Programming

Worked examplesSFINAE — substitution failure is not an error

2,818 words13 min read↑ Read in English

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

Kisi bhi example se pehle, ek reminder simple words mein. Ek template ek recipe hai jisme ek blank T hota hai. Substitution = compiler ek real type us blank mein paste karta hai. Signature ek function ka "header" hota hai — uska return type aur parameter types, woh part jo { } se pehle likha hota hai. Body woh { ... } wala part hai. SFINAE sirf header ko dekhta hai.


The scenario matrix

Har situation jo SFINAE face kar sakta hai, un cells mein se kisi ek mein aati hai:

# Cell class Kya diya jaata hai Expected outcome
1 Valid substitution aisa type jo signature mein fit ho jaata hai candidate rehta hai, compile hota hai
2 Failure in signature aisa type jisme header mein named member missing ho candidate silently drop ho jaata hai (SFINAE)
3 Failure in body header theek hai, { } mein missing member use hoti hai hard error — NOT SFINAE
4 enable_if = true condition hold karti hai → ::type exist karta hai overload enabled
5 enable_if = false condition fail hoti hai → koi ::type nahi overload disabled (SFINAE)
6 Overlapping conditions do overloads dono valid hain ambiguous hard error
7 Degenerate / empty input empty container par .size() valid — 0 return karta hai, koi SFINAE involved nahi
8 Limiting: last candidate removed koi viable overload survive nahi karta "no matching function" error
9 Word problem real logging function paths choose karti hai compile time par sahi path pick hota hai
10 Exam twist identical templates ke return type mein enable_if "redefinition" trap

Neeche ke examples har cell ko cover karte hain. Label [Cell N] batata hai konsa cell hai.


Example 1 — Valid substitution [Cell 1]


Example 2 — Failure in the signature [Cell 2]

Dekho Templates and Type Deduction yeh jaanne ke liye ki deduction substitution se pehle kyun hoti hai.


Example 3 — Failure in the body (NOT SFINAE) [Cell 3]

Figure — SFINAE — substitution failure is not an error

Example 4 — enable_if = true [Cell 4]


Example 5 — enable_if = false [Cell 5]


Example 6 — Overlapping conditions = ambiguity [Cell 6]


Example 7 — Degenerate / empty input [Cell 7]


Example 8 — Limiting case: last candidate removed [Cell 8]


Example 9 — Word problem: ek smart logger [Cell 9]


Example 10 — Exam twist: "redefinition" trap [Cell 10]


Recall Kaunse cells kaunsa compiler behaviour produce karte hain?

Silent drop cells 2, 5, 8 mein hota hai (signature failure). Hard error 3 mein (body), 6 mein (ambiguous), 10-naive mein (redefinition). Cells 1, 4, 7, 9 sab compile aur run hote hain. Message ko mechanism se match karo: "no matching function" = SFINAE ne candidates remove kiye; "no member named X" = body error; "ambiguous" = overlapping conditions.


Connections

  • SFINAE — substitution failure is not an error (index 5.2.17) — parent
  • Templates and Type Deduction
  • std::enable_if and Tag Dispatch
  • decltype and Trailing Return Types
  • Type Traits (std::is_integral, std::void_t)
  • void_t Detection Idiom
  • Overload Resolution
  • Concepts (C++20)