The expression must be a constant expression: built from literals, sizeof, enum values, #defined constants, etc. — anything the compiler can fully evaluate without running the program.
Because step 2 happens during translation, no instruction is generated — the assertion has no footprint in the final binary. This is the whole point: it's a gatekeeper, not a runtime guard.
Imagine you're building a Lego model from instructions that say "this base must be exactly 4 studs wide." A static_assert is like a smart instruction sheet that refuses to let you start building if your base isn't 4 studs wide — it stops you at setup, not after you've built the whole thing and it topples over. The check happens before the model exists (compile time), so once the model is built, the check has cost nothing and can never be skipped.
Dekho yaar, static_assert ek aisa check hai jo compile time par chalta hai, runtime par nahi. Matlab jab aap program banaa rahe ho (compile kar rahe ho), tabhi compiler dekhta hai ki aapki condition true hai ya nahi. Agar false (zero) hui, to program compile hi nahi hoga aur aapka likha hua message error ke roop mein dikhega. Iska sabse bada fayda: yeh zero runtime cost hai — final binary mein iska koi code hi nahi banta, aur isko skip karna namumkin hai.
Important baat: condition ek constant expression honi chahiye — jaise sizeof(int) == 4, ya #defined values, ya enum values. Aap isme normal variable (int n) nahi daal sakte, kyunki compiler ko answer abhi, compile ke time pe, pata hona chahiye. Agar aapko runtime value check karni hai (jaise user ka input), tab plain assert() use karo. Yaad rakho: STATIC = jo sach compile time pe hi fix hai.
C11/C17 mein static_assert actually ek macro hai jo <assert.h> se aata hai (raw keyword _Static_assert hai), aur message dena zaroori hai. C23 mein yeh proper keyword ban gaya aur message optional ho gaya. Common galti: log #error se sizeof check karne ki koshish karte hain — par #error to preprocessor mein chalta hai, usse sizeof samajh hi nahi aata. Isiliye type-size jaisi cheezein check karne ke liye static_assert perfect hai. Real life use: jab aap maan ke chalte ho ki "int 4 byte ka hai" ya "yeh array ka size enum count ke barabar hai" — toh static_assert se yeh assumption permanently lock kar do, taaki koi galat platform ya galti se badli hui value chupke se bug na bana de.