void a() noexcept; // promises: never throwsvoid b() noexcept(true); // same thingvoid c() noexcept(false); // may throw (this is also the default)void d(); // may throw (default)
Hum rules memorize nahi karte — hum unhe derive karte hain ek single contract se: "koi bhi exception escape nahi ho sakti."
Terminate kyun, rethrow kyun nahi? Kyunki poora point yahi tha ki compiler is function ke liye exception-handling tables skip kar sake. Literally koi unwinding info use karne ke liye ho hi nahi sakti — toh ek hi safe action hai: sab kuch rok do.
Yeh hume conditional noexcept likhne deta hai:
template<class T>void swap(T& a, T& b) noexcept( noexcept(T(std::move(a))) && noexcept(a = std::move(b)) );
Yahan outernoexcept(...) specifier hai; innernoexcept(...) woh operator hai jo bool compute karta hai. "Main noexcept hoon exactly tab jab T ko move karna throw nahi kar sakta."
Socho tum apne teacher ko bolte ho: "MAIN PROMISE KARTA HOON ki is kamre ko cross karte waqt paani ka glass nahi giraunga." Agar tum promise nibhao, teacher relax karta hai aur har jagah towels aur mops nahi bikhata — time aur effort bachaata hai. Lekin agar tumne GIRADIYA promise karne ke baad? Tumhe sirf daanta nahi jaata — poori class turant ghar bhej di jaati hai, koi sawaal nahi. noexcept woh promise hai exception "spillage" ke baare mein. Nibhao aur program lean chalega; todo aur program turant band ho jaata hai.
Ki function kisi bhi exception ko apne se bahar (propagate) nahi hone dega.
Agar ek noexcept function exception escape hone de toh kya hota hai?
std::terminate() turant call hota hai; program kill ho jaata hai (koi guaranteed unwinding nahi).
noexcept kis cheez ka shorthand hai?
noexcept(true) ka.
Ek ordinary function ki default exception specification kya hai?
noexcept(false) — yeh throw kar sakta hai.
noexcept(expr)operator kya return karta hai?
Ek compile-time bool: true agar expr guaranteed non-throwing ho, warna false. expr unevaluated hota hai.
std::vector kyun care karta hai ki kisi type ka move constructor noexcept hai ya nahi?
Agar move noexcept hai toh reallocation par elements move karta hai (fast); warna strong exception guarantee preserve karne ke liye copy karta hai (slow).
noexcept specifier aur noexcept operator mein kya fark hai?
Specifier (signature ke baad) ek promise karta hai; operator noexcept(x) bool evaluate karta hai. Same keyword, do roles.
C++11 se destructors kya by default noexcept hain?