5.2.23 · D3 · HinglishC++ Programming

Worked examplesstd - function and std - bind

2,246 words10 min read↑ Read in English

5.2.23 · D3 · Coding › C++ Programming › std - function and std - bind

Yeh page ek drill hai. Hum har us shape ko enumerate karte hain jis situation mein std::function aur std::bind tumhare saamne aa sakti hain, phir har shape ka ek example work karte hain. Agar tumne abhi tak wrapper idea nahi padha, toh pehle parent std::function and std::bind padho — yeh page assume karta hai ki tum jaante ho ki std::function<R(Args...)> koi bhi callable store karta hai jo signature se match kare, aur std::bind arguments pehle se fill kar deta hai.

Shuru karne se pehle, ek word jo hum baar baar use karenge: arity. Kisi callable ki arity simply kitne arguments leti hai hoti hai. sub(a,b) ki arity 2 hai. Jab hum bind karte hain, toh hum arity lower karte hain — hum kam arguments dete hain kyunki kuch pehle se yaad ho jaate hain. Yeh picture dhyan mein rakho:

Figure — std - function and std - bind

The scenario matrix

Har case neeche ek cell hai. Worked examples labeled hain us cell ke saath jo wo hit karta hai, aur milke poora grid cover karte hain.

Cell Kya stress karta hai Callable kind bind action
C1 Ek variable mein alag-alag callables store aur swap karo free fn / lambda / functor none
C2 Callbacks ka container (heterogeneous types) lambdas none
C3 Ek argument fix karo (arity 2 → 1) free fn fix + _1
C4 Arguments reorder karo (arity change nahi) free fn _2, _1
C5 Ek argument bilkul drop karo (arity kam, unused call arg) free fn fix only
C6 Member function bind karo (hidden this) member fn object + _1
C7 Reference semantics — copy vs std::ref lambda over state fix by ref
C8 Degenerate: empty std::function call karo none none
C9 Limiting: arity 0, aur nesting bind-in-bind free fn _1 reuse
C10 Exam twist — side effects ka evaluation order free fn _1, _1

C1 — Ek variable, teen creatures


C2 — Callbacks ka container


C3 — Ek argument fix karo (arity 2 → 1)


C4 — Arguments reorder karo (arity unchanged)


C5 — Ek argument bilkul drop karo


C6 — Member function bind karo (hidden this)


C7 — Copy vs std::ref (the reference trap)


C8 — Degenerate: empty std::function call karna


C9 — Limiting cases: arity 0, aur nesting


C10 — Exam twist: side effects aur placeholder reuse


Recall Matrix ko memory se cover karo

Kaunsa cell argument drop karna test karta hai? ::: C5 — sab slots fixed, extra call args ignore ho jaate hain. Kaunsa cell throw karta hai? ::: C8 — empty std::function call karna std::bad_function_call throw karta hai. std::bind ke through real reference kaise rakhte hain? ::: Arg ko std::ref mein wrap karo (C7); bind by default copy karta hai. Kya ek bind expression mein _1 do baar appear ho sakta hai? ::: Haan — yeh substitution rule hai, toh wahi call arg har _1 slot ko feed karta hai (C9b).

Dekho bhi: Lambda Expressions, Function Pointers, Functors and operator(), Templates and Type Erasure, Callbacks and Event Systems.