Passing a member function of another class into a std::function parameter - c++

I have a class with a function that takes a std::function and stores it. This part seems to compile ok (but please point out any issue if there are any)
#include <functional>
#include <iostream>
struct worker
{
std::function<bool(std::string)> m_callback;
void do_work(std::function<bool(std::string)> callback)
{
m_callback = std::bind(callback, std::placeholders::_1);
callback("hello world\n");
}
};
// pretty boring class - a cut down of my actual class
struct helper
{
worker the_worker;
bool work_callback(std::string str)
{
std::cout << str << std::endl;
return true;
}
};
int main()
{
helper the_helper;
//the_helper.the_worker.do_work(std::bind(&helper::work_callback, the_helper, std::placeholders::_1)); // <---- SEGFAULT (but works in minimal example)
the_helper.the_worker.do_work(std::bind(&helper::work_callback, &the_helper, std::placeholders::_1)); // <---- SEEMS TO WORK
}
I get a segfault, but I am not sure why. I have used this before, in fact, I copied this example from another place I used it. The only real difference that the member function was part of the class I called it from (i.e. this instead of the_helper).
So this is why I am also asking if there is anything else I am doing wrong in general? Like should I be passing the std::function as:
void do_work(std::function<bool(std::string)>&& callback)
or
void do_work(std::function<bool(std::string)>& callback)

As also noted by #Rakete1111 in comments, the problem probably was in this code:
bool work_callback(std::string str)
{
std::cout << str << std::endl;
}
In C++ if a non-void function does not return a value the result is undefined behavior.
This example will crash with clang but pass with gcc.
If helper::work_callback returns (e.g, true) the code works just fine.

I don't know why your code seg faults because I was spoiled and skipped std::bind straight to lambdas. Since you use C++11 you should really convert your code from std::bind to lambdas:
struct worker
{
std::function<bool(std::string)> m_callback;
void do_work(std::function<bool(std::string)> callback)
{
m_callback = callback;
callback("hello world\n");
}
};
Now with work_callback and calling do_work things need some analysis.
First version:
struct helper
{
worker the_worker;
bool work_callback(std::string)
{
return false;
}
};
int main()
{
helper the_helper;
the_helper.the_worker.do_work([&](std::string s) { return the_helper.work_callback(s); });
}
Now this version works with your toy example. However out in the wild you need to be careful. The lambda passed to do_work and then stored in the_worker captures the_helper by reference. This means that this code is valid only if the helper object passed as reference to the lambda outlives the worker object that stores the m_callback. In your example the worker object is a sub-object of the the helper class so this is true. However if in your real example this is not the case or you cannot prove this, then you need to capture by value.
First attempt to capture by value (does not compile):
struct helper
{
worker the_worker;
bool work_callback(std::string)
{
return false;
}
};
int main()
{
helper the_helper;
the_helper.the_worker.do_work([=](std::string s) { return the_helper.work_callback(s); });
}
This does not compile because the copy of the_helper stored in the lambda object is const by default and as such you cannot call work_callback on it.
A questionable solution if you can't make work_callback const is to make the lambda mutable:
struct helper
{
worker the_worker;
bool work_callback(std::string)
{
return false;
}
};
int main()
{
helper the_helper;
the_helper.the_worker.do_work([=](std::string s) mutable { return the_helper.work_callback(s); });
}
But you need to think if this is what you intended.
What would make more sense is to make work_callback const:
struct helper
{
worker the_worker;
bool work_callback(std::string) const
{
return false;
}
};
int main()
{
helper the_helper;
the_helper.the_worker.do_work([=](std::string s) { return the_helper.work_callback(s); });
}

The reason for getting SEGFAULT has been already mentioned in the comments.
However, I would like to point out that, you need to use neither std::bind nor std::function, here in your given case. Instead, simply having a lambda and a function pointer you can handle what you intend to do.
struct worker
{
typedef bool(*fPtr)(const std::string&); // define fun ptr type
fPtr m_callback;
void do_work(const std::string& str)
{
// define a lambda
m_callback = [](const std::string& str)
{
/* do something with string*/
std::cout << "Call from worker: " << str << "\n";
return true;
};
bool flag = m_callback(str);// just call the lambda here
/* do some other stuff*/
}
};
struct helper
{
worker the_worker;
bool work_callback(const std::string& str)
{
std::cout << "Call from helper: ";
this->the_worker.do_work(str);
return true; ------------------------>// remmeber to keep the promise
}
};
And use case would be:
int main()
{
helper the_helper;
the_helper.work_callback(std::string("hello world"));
// or if you intend to use
the_helper.the_worker.do_work(std::string("hello world"));
return 0;
}
see Output here:
PS: In the above case, if worker does not required m_callback for later cases(i.e, only for do_work()), then you can remove this member, as lambdas can be created and called at same place where it has been declared.
struct worker
{
void do_work(const std::string& str)
{
bool flag = [](const std::string& str)->bool
{
/* do something with string*/
std::cout << "Call from worker: " << str << "\n";
return true;
}(str); -------------------------------------> // function call
/* do other stuff */
}
};

Related

Can I capture lambda variables without std::function?

Is it possible to get the captured values of a lambda without using std::function? I'm asking because I want to place the captured copies into my own memory, which std::function cannot do as they don't support custom allocators.
(I assume allocator support missing for std::function is due to a very good reason, perhaps the logic behind capturing values in a lambda is extremely difficult to implement? But if it's possible, I'd like to try it myself.)
Background: I'm asking to learn more about lambda in C++. I'd like to place the captured values and reference pointers in over-aligned memory for a thread pool system I'm writing as an academic exercise. I also really like the brevity writing lambdas with automatic captures, it'd make for a very easy "job" writing interface I think.
class MyFunction {
public:
// I want more than just the function pointer,
// I also want the captured value copies or references too
// I'm unsure how to really accomplish this though.
MyFunction & operator=( ??? ) {
???
}
};
int main(){
int captureThis = 1;
MyFunction func = [=]()->void {
printf("%i", captureThis);
};
}
Checking values of captured variables outside of lambda would not look good but at least you can use a factory function to produce it with its unique "lambda" template type (also with help of auto on the final type) so that you can do extra work between what thread calls and what you initialize:
#include <iostream>
#include <thread>
#include <vector>
template <typename F>
struct Callable
{
Callable(const F && lambda):func(std::move(lambda))
{
}
// this is what the std::thread calls
void operator()()
{
// you can check the captured variable
int out;
func(out,false);
std::cout<< "Callable successfully found out the value of captured variable: "<<out <<std::endl;
func(out,true);
}
const F func;
};
template<typename F>
Callable<F> CallableFactory(F&& lambda)
{
return Callable<F>(std::forward<F>(lambda)); // or std::move(lambda)
}
int main()
{
// variable to capture
int a=1;
auto callable = CallableFactory([&](int & outputCapturedValue, bool runNow){
// not looking good as its not possible from outside (because they are private variables & depends on implementation of C++)
// if checking the captured variables
if(!runNow)
{
outputCapturedValue = a;
std::cout << "inside the lambda: a=" << a <<std::endl;
}
else
{
std::cout<<"algorithm runs"<<std::endl;
}
});
std::vector<std::thread> threads;
threads.emplace_back(std::move(callable));
threads[0].join();
return 0;
}
output:
inside the lambda: a=1
Callable successfully found out the value of captured variable: 1
algorithm runs
If its only for having an array of lambdas processed by array of threads, you can use smart-pointers and an extra container struct to box/unbox them during work-distribution:
#include <iostream>
#include <thread>
#include <vector>
#include <memory>
struct ICallable
{
virtual void operator()()=0;
};
template <typename F>
struct Callable:public ICallable
{
Callable(const F && lambda):func(std::move(lambda))
{
}
// this is what the std::thread calls
void operator()() override
{
func();
}
const F func;
};
template<typename F>
std::shared_ptr<ICallable> CallablePtrFactory(F&& lambda)
{
return std::shared_ptr<ICallable>(new Callable<F>(std::forward<F>(lambda)));
}
struct CallableContainer
{
std::shared_ptr<ICallable> callable;
void operator()()
{
callable.get()->operator()();
}
};
int main()
{
// variable to capture
int a=1;
// simulating work pool
std::vector<std::shared_ptr<ICallable>> callables;
callables.push_back(CallablePtrFactory([&](){
std::cout<< "a="<<a<<std::endl;
}));
// simulating worker pool load-balancing
std::vector<std::thread> threads;
threads.emplace_back(CallableContainer{ callables[0] });
threads[0].join();
return 0;
}
output:
a=1
If you're after a custom-allocation for the container, you can just use a second parameter for the factory function. Following example uses placement-new on a stack buffer. But still the lambda itself has something else outside of it making container's size not changed by its lambda (just like a function-pointer):
#include <iostream>
#include <thread>
#include <vector>
#include <memory>
struct ICallable
{
virtual void operator()()=0;
};
template <typename F>
struct Callable:public ICallable
{
Callable(const F && lambda):func(std::move(lambda))
{
currentSize = sizeof(*this); std::cout<<"current size = "<<currentSize <<" (useful for alignement of next element?)" <<std::endl;
}
// this is what the std::thread calls
void operator()() override
{
func();
}
int currentSize;
const F func;
};
template<typename F>
std::shared_ptr<ICallable> CallablePtrFactory(F&& lambda, char * buffer)
{
return std::shared_ptr<ICallable>(
new (buffer) Callable<F>(std::forward<F>(lambda)),
[](ICallable *){ /* placement-new does not require a delete! */}
);
}
struct CallableContainer
{
std::shared_ptr<ICallable> callable;
void operator()()
{
callable.get()->operator()();
}
};
int main()
{
// variable to capture
int a=1;
char buffer[10000];
// simulating work pool
std::vector<std::shared_ptr<ICallable>> callables;
callables.push_back(
// observe the buffer for placement-new
CallablePtrFactory([&](){
std::cout<< "a="<<a<<std::endl;
},buffer /* you should compute offset for next element */)
);
// simulating worker pool load-balancing
std::vector<std::thread> threads;
threads.emplace_back(CallableContainer{ callables[0] });
threads[0].join();
return 0;
}
output:
current size = 24 (useful for alignement of next element?)
a=1
Considering the thread pool system you mention in comments then you could try a polymorphic approach:
class ThreadRoutine
{
protected:
virtual ~ThreadRoutine() { }
public:
virtual void run() = 0;
};
template <typename Runner>
class ThreadRoutineT
{
// variant 1:
Runner m_runner; // store by value;
// variant 2:
std::reference_wrapper<Runner> m_runner;
public:
void run() override { m_runner(); } // call operator()
};
Now you might store your thread routines in a std::vector (note: pointers to, by value would lead to object slicing; likely std::unique_ptr, possibly, depending on use case, a classic raw pointer might fit, too).
You could even implement both variants, your thread pool manager could provide an additional parameter in the thread creation function or maybe even more elegant distinguish by overloading that function (l-value reference: create the reference wrapper variant; r-value reference: create the value variant, move-construct it), e.g. like:
class ThreadManager
{
template <typename Runner>
void createThread(Runner& runner)
{
// assuming std::vector<std::unique_ptr<ThreadRoutine>>
m_runners.emplace_back
(
// assuming appropriate constructor
std::make_unique<ThreadRoutineRef>(runner)
);
// return some kind of thread handle or start thread directly?
// thread handle: could be an iterator into a std::list
// (instead of std::vector) as these iterators do not invalidate
// if elements preceding in the list are removed
// alternatively an id as key into a std::[unordered_]map
}
template <typename Runner>
void createThread(Runner&& runner)
{
m_runners.emplace_back
(
// assuming appropriate constructor
std::make_unique<ThreadRoutineVal>(std::move(runner))
);
}
}
About the alignment issue: The specific template instantiations would select the alignment appropriate for the template argument type, so you wouldn't have to consider anything particular.

c++ callbacks to another member function

I have a question on callbacks. Previously, I am associating my callbacks to a class Q
class Q{
using Callback = std::function<void(char*, int)>;
Q:Q();
Q:~Q();
void Q::RegisterCB(Callback callbackfunc)
{
callback_func = callbackfunc;
}
void Q:someEvent()
{
callback_func();
}
};
void handleCallback( char*, int)
{
// perform some routine
}
// from my main file
int main()
{
Q q;
q.RegisterCB(&handleCallback);
}
It works well for me. However, when I need to transfer the handleCallback function to another class for cleaner code. I have problem with using same code
class R{
void R::handleCallback( char*, int)
{
// perform some routine
}
void R::someOp()
{
// q is some member variables of R
q.RegisterCB(&R::handleCallback, this);
}
};
However, i run into some problems of saying there is a "no matching function for call to .....". I thought it was just simply assigning from function name to class function name
May I have a hint to where I might go wrong?
Regards
&R::handleCallback has the type void (R::*)(char*, int), which is not convertible to std::function<void(char*, int)>.
Also, RegisterCB takes one argument, not two.
The most straightforward fix is to wrap the call in a lambda function,
q.RegisterCB([this](char* p, int x) { handleCallback(p, x); });
Example on how to use a lambda function to register a member function of an instance of R as event handler. (I replaced char* with string_view out of habit, it's not essential for this example). The use of "const" wherever you can is a recommendation.
#include <functional>
#include <string_view>
#include <iostream>
class Q
{
public:
// use const arguments, the callback is not supposed to change them
// just passing information on to callback
using callback_t = std::function<void(const std::string_view&, const int)>;
// initialize callback with a (lambda) function that does nothing
// this prevents the need for a check if callback has been set or not
// (Pattern : Null Strategy)
Q() :
m_callback_func( [](const std::string_view&,const int) {} )
{
}
~Q() = default;
void RegisterCallback(callback_t fn)
{
m_callback_func = fn;
}
void Event(const std::string_view& string, const int value)
{
m_callback_func(string,value);
}
private:
callback_t m_callback_func;
};
void handleCallback(const std::string_view& string, const int value)
{
std::cout << string << ", " << value << "\n";
}
class R
{
public:
void handleCallback(const std::string_view& string, const int value)
{
std::cout << string << ", " << value << "\n";
}
};
// from my main file
int main()
{
Q q1;
q1.RegisterCallback(handleCallback);
q1.Event("Hello", 42);
// to pass a callback to an instance of a class
// you can use a lambda function https://en.cppreference.com/w/cpp/language/lambda
R r;
Q q2;
q2.RegisterCallback([&r](const std::string_view& string, const int value)
{
r.handleCallback(string,value);
});
q2.Event("World",21);
return 0;
}

Creating a callback with std::function as class-member

I have designed a simple callback-keyListener-"Interface" with the help of a pure virtual function. Also I used a shared_ptr, to express the ownership and to be sure, that the listener is always available in the handler. That works like a charme, but now I want to implement the same functionality with the help of std::function, because with std::function I am able to use lambdas/functors and I do not need to derive from some "interface"-classes.
I tried to implement the std::function-variant in the second example and it seems to work, but I have two questions related to example 2:
Why does this example still work, although the listener is out of scope? (It seems, that we are working with a copy of the listener instead of the origin listener?)
How can I modify the second example, to achieve the same functionality like in the first example (working on the origin listener)? (member-ptr to std::function seems not to work! How can we handle here the case, when the listener is going out of scope before the handler? )
Example 1: With a virtual function
#include <memory>
struct KeyListenerInterface
{
virtual ~KeyListenerInterface(){}
virtual void keyPressed(int k) = 0;
};
struct KeyListenerA : public KeyListenerInterface
{
void virtual keyPressed(int k) override {}
};
struct KeyHandler
{
std::shared_ptr<KeyListenerInterface> m_sptrkeyListener;
void registerKeyListener(std::shared_ptr<KeyListenerInterface> sptrkeyListener)
{
m_sptrkeyListener = sptrkeyListener;
}
void pressKey() { m_sptrkeyListener->keyPressed(42); }
};
int main()
{
KeyHandler oKeyHandler;
{
auto sptrKeyListener = std::make_shared<KeyListenerA>();
oKeyHandler.registerKeyListener(sptrKeyListener);
}
oKeyHandler.pressKey();
}
Example 2: With std::function
#include <functional>
#include <memory>
struct KeyListenerA
{
void operator()(int k) {}
};
struct KeyHandler
{
std::function<void(int)> m_funcKeyListener;
void registerKeyListener(const std::function<void(int)> &funcKeyListener)
{
m_funcKeyListener = funcKeyListener;
}
void pressKey() { m_funcKeyListener(42); }
};
int main()
{
KeyHandler oKeyHandler;
{
KeyListenerA keyListener;
oKeyHandler.registerKeyListener(keyListener);
}
oKeyHandler.pressKey();
}
std::function<Sig> implements value semantic callbacks.
This means it copies what you put into it.
In C++, things that can be copied or moved should, well, behave a lot like the original. The thing you are copying or moving can carry with it references or pointers to an extrenal resource, and everything should work fine.
How exactly to adapt to value semantics depends on what state you want in your KeyListener; in your case, there is no state, and copies of no state are all the same.
I'll assume we want to care about the state it stores:
struct KeyListenerA {
int* last_pressed = 0;
void operator()(int k) {if (last_pressed) *last_pressed = k;}
};
struct KeyHandler {
std::function<void(int)> m_funcKeyListener;
void registerKeyListener(std::function<void(int)> funcKeyListener) {
m_funcKeyListener = std::move(funcKeyListener);
}
void pressKey() { m_funcKeyListener(42); }
};
int main() {
KeyHandler oKeyHandler;
int last_pressed = -1;
{
KeyListenerA keyListener{&last_pressed};
oKeyHandler.registerKeyListener(keyListener);
}
oKeyHandler.pressKey();
std::cout << last_pressed << "\n"; // prints 42
}
or
{
oKeyHandler.registerKeyListener([&last_pressed](int k){last_pressed=k;});
}
here we store a reference or pointer to the state in the callable. This gets copied around, and when invoked the right action occurs.
The problem I have with listeners is the doulbe lifetime issue; a listener link is only valid as long as both the broadcaster and reciever exist.
To this end, I use something like this:
using token = std::shared_ptr<void>;
template<class...Message>
struct broadcaster {
using reciever = std::function< void(Message...) >;
token attach( reciever r ) {
return attach(std::make_shared<reciever>(std::move(r)));
}
token attach( std::shared_ptr<reciever> r ) {
auto l = lock();
targets.push_back(r);
return r;
}
void operator()( Message... msg ) {
decltype(targets) tmp;
{
// do a pass that filters out expired targets,
// so we don't leave zombie targets around forever.
auto l = lock();
targets.erase(
std::remove_if( begin(targets), end(targets),
[](auto&& ptr){ return ptr.expired(); }
),
end(targets)
);
tmp = targets; // copy the targets to a local array
}
for (auto&& wpf:tmp) {
auto spf = wpf.lock();
// If in another thread, someone makes the token invalid
// while it still exists, we can do an invalid call here:
if (spf) (*spf)(msg...);
// (There is no safe way around this issue; to fix it, you
// have to either restrict which threads invalidation occurs
// in, or use the shared_ptr `attach` and ensure that final
// destruction doesn't occur until shared ptr is actually
// destroyed. Aliasing constructor may help here.)
}
}
private:
std::mutex m;
auto lock() { return std::unique_lock<std::mutex>(m); }
std::vector< std::weak_ptr<reciever> > targets;
};
which converts your code to:
struct KeyHandler {
broadcaster<int> KeyPressed;
};
int main() {
KeyHandler oKeyHandler;
int last_pressed = -1;
token listen;
{
listen = oKeyHandler.KeyPressed.attach([&last_pressed](int k){last_pressed=k;});
}
oKeyHandler.KeyPressed(42);
std::cout << last_pressed << "\n"; // prints 42
listen = {}; // detach
oKeyHandler.KeyPressed(13);
std::cout << last_pressed << "\n"; // still prints 42
}

Is there a possibility to make a function wrapper that runs the injected code and returns the same data as the injected function?

I am toying with this idea for a while but cant seem to wrap my head around it.
Basically what I want to do is create a general Timer class that times all the functions that are passed to it. Averaging it when the same function is called multiple times so it has to store it somehow. It should therefore use the function name to store the task and average it when it occurs more than once.
Pseudoish code of what it should look like.
Class FunctionTaks
{
std::string d_name;
double d_execution_time;
}
Class Timer
{
private:
std::vector<FunctionTask> d_tasks;
public:
template <typename Function, typename ReturnType>
ReturnType time(Function f)
{
// check if function f is timed for first time
// start timer
// run function f
auto r = f.invoke();
// stop timer
// store function name and time, average if more than once
// return whatever f should return
return r;
}
void report() const;
}
I dont really know how to do this, especially when Function f has a different amount of arguments.
Timer t;
t.time(foo());
t.time(bar());
t.time(foo());
t.report();
I basically have a few core issues.
How to let a function wrapper return the same type that the injected code is suppose to return.
How to obtain the function name that is being injected.
The wrapper should not be limited by arguments passed on to the injected function. How to give the injected function the freedom of arguments.
On the other hand I dont really care about the arguments and return type, the wrapper should simply run the injected function as is and perform some timings and then return whatever the injected function is suppose to return.
C++11 but why templates? You need lambda expressions:
typedef void(*TimerFunction)();
void time(TimerFunction fun) {
// start timer
fun();
// stop timer
}
int fun1() { return 1; }
int fun2() { return 2; }
string fun3() { return string("Test"); }
main() {
int ret1, ret2;
string ret3;
t.time([&ret1]() { ret1 = fun1(); });
t.time([&ret2]() { ret2 = fun2(); });
t.time([&ret3]() { ret3 = fun3(); });
}
That's the concept. For details: C++ lambda with captures as a function pointer
With C++11 you can use variable template parameters:
class TimerFoo {
public:
template <class Foo, class... Args> TimerFoo(Foo foo, Args... args) {
// ... start timer
foo(args...);
// ... stop timer
}
};
And use e.g.:
TimerFoo tf = TimerFoo(foo, 1, 2, 3);
Ofcourse you need some field in TimerFoo that will store the measured time...
Edit:
To be able to return a value of your function using this approach you could change the above code to:
#include <iostream>
using namespace std;
class TimerFoo {
public:
template <class Foo, class... Args> auto run(Foo foo, Args... args) -> decltype(foo(args...)) {
// ... start timer
auto result = foo(args...);
// ... stop timer
return result;
}
};
int foo(int a, int b) {
return 2;
}
int main() {
TimerFoo tf;
cout << tf.run(foo, 1, 2) << endl; // output: 2
}

One function that can use dynamic function swapping

This might sound a little convoluted, but here we go.
So, I have the following code:
void Utility::validateRangeAndModify(Pet pet, int checkint,
int numbertovalidate,
bool greaterorless)
{
if (greaterorless) {
if (numbertovalidate < checkint)
pet.getAttributes()->setPetHunger(0);
} else
if (numbertovalidate > checkint)
pet.getAttributes()->func(100);
}
Firstly, This code is designed to validate a single integer. That part is easy.
Then, what I want it to do is carry out a function depending on if the integer meets the condition or not. In this case, setPetHunger() is being set to either 0 or 100. The issue is, I have setPetHealth(), and setPetEnergy() too.
The function I want it to perform is the thing that I want to change.
For instance. This code will only work for my pets Hunger. It won't work for it's Health, Happiness, or any of it's other variables. and I have a ton of other variables.
I'm wondering if there is any way to achieve something like this:
void Utility::validateRangeAndModify(Pet pet,
int checkint,
int numbertovalidate,
bool greaterorless,
string functiontouse)
{
if (greaterorless) {
if (numbertovalidate < checkint)
pet.getAttributes()->setPetHunger(0);
} else
if (numbertovalidate > checkint)
pet.getAttributes()->FUNCTION_TO_USE(100);
}
I could use something like reflection for this in C#. However, I don't know an alternative function in c++
You can do it by passing the correct member function pointer as parameter to Utility::validateRangeAndModify as below:
void
Utility::validateRangeAndModify(Pet &pet, int checkint, int numbertovalidate,
bool greaterorless, void(Attrib::*memfun)(int)) {
^^^^^^^^^^^^^^^^^^^^^^^^^^
if(greaterorless) {
if(numbertovalidate < checkint) (pet.getAttributes()->*memfun)(0);
} else {
if(numbertovalidate < checkint) (pet.getAttributes()->*memfun)(100);
}
}
And then call it as (if Utility::validateRangeAndModify is not static use obj.validateRangeAndModify):
Utility::validateRangeAndModify(p, 10, 9, false, &Attrib::setPetHunger);
Utility::validateRangeAndModify(p, 10, 9, false, &Attrib::setPetThirst);
LIVE DEMO
You should use a function pointer:
void Utility::validateRangeAndModify(Pet pet, int checkint,
int numbertovalidate,
bool greaterorless,
void (*func)(int, attribs &))
{
if (greaterorless) {
if (numbertovalidate < checkint)
pet.getAttributes()->setPetHunger(0);
} else
if (numbertovalidate > checkint)
func(100, pet.getAttribs());
}
You and the client will agree on the prototype of the function that you will pass; in this case, the prototype will be void func(int, attribs &);
The client can write code like this:
void happiness(int level, attribs &a)
{
// some code here
}
Utility::validateRangeAndModify(..., happiness);
Now happiness willl be called by your validateRangeAndModify function.
NOTE: happiness is a free function. If it is a member function, mark it as static: then there won't be the extra this argument.
To keep this simple, I'm providing a really trivial program that uses std::bind, std::function, and std::placeholders to allow calls to a bound method. This keeps the correct this with the method and generally prevents nastiness.
#include <iostream>
#include <functional>
// bait class to catch bound function calls. Replace this with pet for OP's example
class TwoMethods
{
public:
void method1(int val)
{
std::cout << "Method 1 received " << val << std::endl;
}
void method2(int val)
{
std::cout << "Method 2 received " << val << std::endl;
}
};
// utility function that will operate on a bound function
void utilityfunction(std::function<void(int)> func)
{
// calls passed function. Quietly handles bound this parameter and replaces
// placeholder with 42
func(42);
}
int main()
{
TwoMethods obj;
// call utility function with appropriate object and method
// the std::placeholders::_1 lets the compiler know there is another parameter that
// will be supplied later
utilityfunction(std::bind(&TwoMethods::method1, obj, std::placeholders::_1));
utilityfunction(std::bind(&TwoMethods::method2, obj, std::placeholders::_1));
}
In OP's case:
void Utility::validateRangeAndModify(int checkint,
int numbertovalidate,
bool greaterorless,
std::function<void(int)> func)
{
switch (greaterorless)
{
case 0:
{
if (numbertovalidate < checkint)
{
func(0);
}
}
break;
case 1:
{
if (numbertovalidate > checkint)
{
func(100);
}
break;
}
}
}
And called something like:
validateRangeAndModify(42, 666, false,
std::bind(&Attributes::setPetHunger,
pet.getAttributes(),
std::placeholders::_1))