std::bind
is a function adapter in C++ that allows you to create a
callable object by binding one or more arguments to a function or function
object.
- It is part of the
<functional>
header and provides a way to create a new
function that can be invoked with a specific set of arguments, while leaving
some arguments unbound.
std::bind
is useful when you need to adapt a function to match a specific
signature for algorithms or event-driven programming.
- It can also be used to create a function that calls another function with a
fixed set of arguments, reducing the need to repeatedly pass those arguments.
// (1) (since C++11) (constexpr since C++20).
template< class F, class... Args >
/* unspecified */ bind( F&& f, Args&&... args );
// (2) (since C++11) (constexpr since C++20).
template< class R, class F, class... Args >
/* unspecified */ bind( F&& f, Args&&... args );
- Partial function application:
RetType funName( Type1 para1, Type2 para2, ... ) { ...; };
Type2 arg2;
auto boundName = std::bind( funName, arg2, std::placeholders::_1, ... );
- Working with callbacks or functional interfaces:
RetType funName( Type1 para1, Type2 para2, ... ) { ...; };
auto boundName = std::bind( funName, arg1, arg2, ... );
std::thread thread_name( boundName );
- Binding member functions:
class ClassName {
public:
RetType funcName( Type1 para1, ... ) const { ...; };
};
int main() {
ClassName obj_name;
auto boundName = std::bind( &ClassName::funcName, obj_name, arg1, ... );
boundName();
return 0;
};
- Reordering arguments:
RetType funcName( Type1 para1, Type2 para2, ... ) { ...; };
int main() {
auto boundName = std::bind(
funcName, std::placeholders::_2, std::placeholders::_1, ... );
boundName( arg2, arg1 );
};
- Avoiding verbose lambda expressions.
- Compatibility with legacy code:
std::bind1st/std::bind2nd
(from C++98).
std::bind
in cplusplus.
std::bind
in cppreference.
std::function
is a template class in C++ that provides a type-safe
wrapper for any callable entity, including functions, function pointers,
and function objects.
- It is part of the header and allows you to store and invoke
callable objects with a specific signature.
std::function
is useful for scenarios where you need to store or pass
around functions as first-class objects, such as callback functions, event
handlers, or when working with algorithms that require generic function
signatures.
- For high-performance code where overhead is a concern, function pointers or
direct calls may be more suitable.
// (1) (since C++11).
template< class > class function; /* undefined */
// (2) (since C++11).
template< class R, class... Args > class function< R( Args... ) >;
- Type-erased function wrapper:
RetType funcName( Type1 para1, Type2 para2, ... ) { ...; };
int main() {
std::function< RetType( Type1, Type2, ... ) > func = funcName;
func(); // Calls funcName().
};
- Storing different types of callables:
RetType funcName1() { ...; };
struct FuncName2 {
RetType operator()( ... ) const { ...; };
};
int main() {
std::function< RetType( ... ) > f1 = funcName1; // Function pointer.
std::function< RetType( ... ) > f2 = FuncName2( ... ); // Functor.
std::function< RetType( ... ) > f3 = [...]( ... ) { // Lambda.
...;
};
f1(); // Calls funcName1.
f2(); // Calls FuncName2().
f3(); // Calls the lambda.
return 0;
};
- Use in callbacks and event handlers.
- Memory and performance considerations: Dynamic memory allocation.
- Function composition and higher-order functions.
- Use in containers.
- Flexible function signatures.
- Passing functions as arguments.
- Using
std::function
with member functions:
class ClassName {
public:
RetType funcName( Type1 para1, Type2 para2, ... ) const { ...; };
};
int main() {
ClassName obj_name;
std::function< RetType( const ClassName&, Type1, Type2, ... ) > obj_nameFunc
= &ClassName::funcName;
obj_nameFunc( obj_name, arg1, arg2, ... );
};
std::function
in cplusplus.
std::function
in cppreference.