5.2.23 · HinglishC++ Programming

std - function and std - bind

1,817 words8 min readRead in English

5.2.23 · Coding › C++ Programming


YE EXIST KYUN KARTA HAI?


std::function


std::bind

Placeholders arguments ko HOW map karte hain

Jab aap baad mein bound object ko arguments ke saath call karte ho:

  • Bind expression mein ek fixed value as-is use hoti hai.
  • Ek placeholder _n ko n-th call argument se replace kiya jaata hai.
Figure — std - function and std - bind

bind vs lambda (modern sach)


Common galtiyan


Recall Feynman: 12 saal ke bacche ko explain karo

Ek TV remote (std::function) socho. Uske buttons ek fixed shape ke hain (signature). Tumhe parwah nahi ki remote Sony se baat karta hai ya Samsung se — button dabaate ho toh kaam hota hai. std::function ek aisa remote hai jo kisi bhi "TV" (function, lambda, etc.) ki taraf point kiya ja sakta hai jo same buttons samajhta ho. Ab std::bind ek button pehle se set karne jaisa hai: tumhare paas ek volume button hai jo "kaunsa TV" aur "kitna loud" leta hai. Tum uspe ek label chipaate ho "hamesha living-room TV" ka, toh ab button sirf "kitna loud?" puchta hai. Tumne jawaab ka ek hissa pehle se yaad kar liya.


Flashcards

std::function<R(Args...)> kya store karta hai?
Koi bhi callable (free fn, lambda, functor, member fn) jiska call signature R(Args...) se match kare, type erasure ke zariye.
Aap do alag lambdas ko seedha same variable mein kyun nahi daal sakte?
Har lambda ka apna unique unnamed type hota hai; std::function unhe ek signature-based type ke under unify karta hai.
Empty std::function call karne par kya hota hai?
Ye std::bad_function_call throw karta hai.
std::bind conceptually kya karta hai?
Partial application: kuch arguments abhi fix karta hai, baaki ke liye placeholders chhodta hai, ek naya callable produce karta hai.
Placeholder _2 ka matlab kya hai?
Bound object baad mein call karte waqt supply kiya gaya doosra argument (bind list mein 2nd item nahi).
std::bind ko argument by reference kaise pass karte ho?
Use std::ref(x) (ya std::cref(x)) mein wrap karo; warna bind value se copy karta hai.
Object c ka member function addBase kaise bind karein?
std::bind(&Counter::addBase, &c, _1) — object/pointer pehla bound argument hota hai (implicit this).
std::function zero-cost kyun nahi hai?
Ye heap-allocate kar sakta hai aur indirect/virtual call use karta hai, isliye direct call ki tarah inline nahi ho sakta.
Modern preference: bind ya lambda?
Lambda — zyada clear aur usually faster; bind zyaadatar legacy code mein milta hai.
Woh bind expression do jo sub(a,b) ke args swap kare.
std::bind(sub, _2, _1).

Connections

  • Lambda Expressions — zyaadatar std::bind uses ka modern replacement
  • Function Pointers — low-level callable jo std::function wrap kar sakta hai
  • Functors and operator() — class-based callables
  • Templates and Type Erasurestd::function ke peeche ka mechanism
  • Callbacks and Event Systemsstd::function ka primary real-world use
  • std::ref and std::crefbind ke zariye references pass karne ke liye zaroori

Concept Map

includes

includes

includes

includes

must match

must match

must match

must match

defines type of

achieved via

unifies types for

if unset

pre-fills args

stored in

Callables in C++

Free function

Lambda unique type

Functor operator

Member function ptr

Call signature R Args

std::function wrapper

Type erasure

Vector of callbacks

Empty throws bad_function_call

std::bind partial application

New callable fewer args