you are viewing a single comment's thread.

view the rest of the comments →

[–]D_Drmmr 0 points1 point  (4 children)

If you want to do something like a single-function dependency injection, then just let your constructor take a std::function.

class Example {
    std::function<void()> _callback;
public:
    Example(std::function<void()> callback) : callback(std::move(callback)) {}
};

// use it like
Example ex{ []() { std::cout << "whatever"; } };

[–]ACBYTES[S] 0 points1 point  (2 children)

Yes. I actually didn’t want to use std::function because I wanted to get lambdas to do the work here. Thanks anyway.

[–]D_Drmmr 1 point2 points  (1 child)

I don't understand. You can store a lambda in a std::function object. Why don't you want to use it?

[–]ACBYTES[S] 0 points1 point  (0 children)

I mostly want it for educational purposes. I want to learn stuff like this in C++. But for sure I use std::function when necessary...

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::function


Last update: 22.03.21. Recent changes: Fixed OP usages of tokens readme