I would like to propose the addition of a utility similar to std::bind from the C++ std. I believe it could be useful to many users as this would allow them to create custom function objects with bound arguments, providing a convenient way to adapt function signatures and pass additional data to callback functions.
A specific use case that inspired this request is a scenario where a user wants to pass stateful functions (e.g., member functions of an object) to another function, which expects a specific function signature.
Example:
Suppose we have a class MyCipher with an internal state:
class MyCipher {
public:
MyCipher(int key) : key_(key) {}
void encrypt(byte* output, const byte* input, size_t len) {
// Perform encryption with the internal key_
}
private:
int key_;
// + Other changing state
};
Now, we have a window function that needs to accept both stateful or stateless objects (to apply our cipher in byte chunks etc):
// Template function that accepts any callable object with the specified signature
template <typename Callable>
void chunkApply(byte* output, const byte* input, size_t len, Callable func) {
// ... More Logic
// Apply the function on the current chunk
func(output, currentChunk, currentChunkLength);
// ...
}
I will look into what is require of std::bind.
It will depend on whether it can be implemented without access to compiler specific implicit or built-in functions.
I would like to propose the addition of a utility similar to std::bind from the C++ std. I believe it could be useful to many users as this would allow them to create custom function objects with bound arguments, providing a convenient way to adapt function signatures and pass additional data to callback functions.
A specific use case that inspired this request is a scenario where a user wants to pass stateful functions (e.g., member functions of an object) to another function, which expects a specific function signature.
Example:
Suppose we have a class MyCipher with an internal state:
Now, we have a window function that needs to accept both stateful or stateless objects (to apply our cipher in byte chunks etc):
Ideally we would just do something like:
But currently we need to do:
And the boiler plate increases with the number of methods we want to pass into this pattern.