5.2.23 · D1 · HinglishC++ Programming

Foundationsstd - function and std - bind

2,192 words10 min read↑ Read in English

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

Pehle aap std::function aur std::bind samjho, usse pehle aapko us vocabulary mein fluent hona chahiye jo parent note assume karta hai. Yeh page un sab cheezein ek-ek karke zero se build karta hai — ek smart 12-saal ka baccha jo C++ kabhi chhuaa nahi usne bhi yeh padhke parent note line by line padh saakna chahiye.


0. "Function call karna" matlab kya hota hai?

Figure — std - function and std - bind

Figure dekho: arguments 2 aur 3 parentheses ke zariye andar jaate hain, function body apna kaam karti hai, aur return value 5 bahar aati hai. Is poore page ki har cheez us choti machine ko — us box ko — capture karne aur idhar-udhar move karne ke baare mein hai.


1. Return type aur parameter types — call ki shape

Concretely, add do ints leta hai aur ek int return karta hai, toh uski signature hai int(int, int).


2. Type — C++ itna fussy kyun hai

Free function add ka type int(*)(int,int) likha jaata hai — padho "pointer to a function taking two ints, returning int". Yeh directly agle symbol par le jaata hai.


3. Function pointer int(*)(int,int)

Figure — std - function and std - bind

Figure mein yellow box p mein adding machine nahi hai; usme usri arrow hai. p(2,3) likhne par arrow follow hota hai aur add run hota hai. Full treatment ke liye dekho Function Pointers.


4. Lambda — ek anonymous function jo aap inline likhte ho


5. Functor — ek object jise aap function ki tarah call kar sako


6. Member function aur hidden this

Figure — std - function and std - bind

7. Templates aur type erasure — box ke peeche ka trick

Figure — std - function and std - bind

Figure mein teen alag shapes ke plugs dikhte hain (function pointer, lambda, functor) — sab ek adapter mein fit ho rahe hain — woh adapter hai std::function. Downstream code sirf adapter ki socket shape (signature) dekhta hai, peeche ka plug nahi.


8. Angle brackets, ..., aur underscore placeholders


9. References, std::ref, aur std::cref


Prerequisite map

Calling a function

Signature R of Args

Types in C plus plus

Function pointers

Lambdas unique type

Functors operator paren

Member functions hidden this

Templates

Type erasure

std function

std bind and placeholders

References and std ref

std function and std bind

Har arrow kehta hai "right wali idea samajhne ke liye left wali idea chahiye". Notice karo ki std::function aur std::bind dono parent topic the parent topic ke bilkul neeche baithe hain — yeh aakhri do bricks hain.


Equipment checklist

Khud test karo — right side chhupaao aur zor se jawab do.

"Function call karna" ka kya matlab hai?
Uska naam aur parentheses mein inputs likho; uske code par jump karo, run karo, return value waapas lo.
Kisi function ki signature kya hoti hai?
Uska return type plus uske parameter types, likha R(Args...).
Do alag lambdas ek variable share kyun nahi kar sakte?
Har lambda ka apna unique unnamed type hota hai, aur C++ ko ek variable ke liye ek type chahiye.
Function pointer kya hota hai?
Ek variable jo kisi function ka memory address hold karta hai, taaki aap function ko uske through call kar sako.
Kisi object ko functor kya banata hai?
Woh operator() define karta hai, toh aap ise obj(args) ki tarah invoke kar sako.
Har member function ko kaunsa hidden argument milta hai?
Woh object jis par woh run karta hai, jise this kehte hain, ek secret first parameter ke roop mein.
Type erasure kya hai?
Kisi value ke concrete type ko ek uniform interface ke peeche chhupaana — yahan, "mujhe Args... ke saath call kar sakte ho aur R milega".
Placeholder _2 ka kya matlab hai?
Woh doosra argument jo caller baad mein bound object call karte waqt supply karega.
std::ref ki zaroorat kyun hai?
Kyunki std::bind arguments ko value se copy karta hai; std::ref(x) reference store karta hai taaki wahi object share ho.
Is poore topic ki ek-line idea kya hai?
std::function ek uniform box hai jo kisi diye gaye signature ke kisi bhi callable ko store karta hai; std::bind uske kuch arguments pehle se fill kar deta hai.

Ready? Toh waapas jao std::function and std::bind par aur har symbol pehle se familiar hoga.