Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

use of callback function in c++14

44 views
Skip to first unread message

kushal bhattacharya

unread,
Jan 6, 2017, 6:37:26 AM1/6/17
to
Is callbackback function used in current application projects(in C++)?If so then how do we define a callback function.My scenario is ,i want to set the callback function in a function first and then when a particular event is being notified i want to get that callback function called.What is the best way to do that
Thanks,
Kushal

Wouter van Ooijen

unread,
Jan 6, 2017, 6:57:09 AM1/6/17
to
Op 06-Jan-17 om 12:37 schreef kushal bhattacharya:
> Is callbackback function used in current application projects(in C++)?If so then how do we define a callback function.My scenario is ,i want to set the callback function in a function first and then when a particular event is being notified i want to get that callback function called.What is the best way to do that
> Thanks,
> Kushal

store the callback as a std::function< void( void ) > and call it when
needed?

Wouter "Objects? No Thanks?" van Ooijen

Alf P. Steinbach

unread,
Jan 6, 2017, 11:02:44 AM1/6/17
to
On 06.01.2017 12:37, kushal bhattacharya wrote:
> Is callbackback function used in current application projects(in
> C++)?

Yes, just like loops are used, and so on. Callback functions are a basic
technique. A main difference between C and C++ is that in C++ a callback
function might be a template argument or associated with a template
argument, and then the calls of it can be inlined, like in `std::sort`
(which is therefore generally faster than C's `qsort`).


> If so then how do we define a callback function.

Depends on the code that's calling it.


> My scenario is, i want to set the callback function in a function
> first and then when a particular event is being notified i want to
> get that callback function called.What is the best way to do that

Depends on your definition of “best”, which is nowhere in sight.

Some possible choices for callback:

* Ordinary free function, possibly with a state argument.

* For a member function + object, the result of `std::mem_fn`.

* A `std::function` object.

* An object of a specified class with a virtual method (the callback).

* Some custom delegate (C# terminology) kind instead of `std::mem_fn`
result; before C++11 there was a plethora of 3rd party such classes.

I think that list is exhaustive or very nearly so.

Callbacks for events are a bit tricky because you run into `const`
correctness issues: differentiating between `const` and non-`const`
objects for event callbacks is usually, IME, not a practical idea.

Cheers!,

- Alf

Scott Lurndal

unread,
Jan 6, 2017, 12:34:19 PM1/6/17
to
"Alf P. Steinbach" <alf.p.stein...@gmail.com> writes:
>On 06.01.2017 12:37, kushal bhattacharya wrote:
>> Is callbackback function used in current application projects(in
>> C++)?
>
>Yes, just like loops are used, and so on. Callback functions are a basic
>technique. A main difference between C and C++ is that in C++ a callback
>function might be a template argument or associated with a template
>argument, and then the calls of it can be inlined, like in `std::sort`
>(which is therefore generally faster than C's `qsort`).
>
>
>> If so then how do we define a callback function.
>
>Depends on the code that's calling it.
>
>
>> My scenario is, i want to set the callback function in a function
>> first and then when a particular event is being notified i want to
>> get that callback function called.What is the best way to do that
>
>Depends on your definition of “best”, which is nowhere in sight.
>
>Some possible choices for callback:
>
>* Ordinary free function, possibly with a state argument.
>
>* For a member function + object, the result of `std::mem_fn`.
>
>* A `std::function` object.
>
>* An object of a specified class with a virtual method (the callback).
>
>* Some custom delegate (C# terminology) kind instead of `std::mem_fn`
>result; before C++11 there was a plethora of 3rd party such classes.

You missed the regular old function pointer method.

e.g.

bool (c_processor::*op_insn)(struct _op *);

//
// If a fault was thrown (longjmp) during operand
// fetch, p_fetch fault will be true and the state of the
// p_operands[] array will be indeterminate and we don't
// want to execute the instruction. Unless it is a non-taken
// branch, that is, which is allowed to have e.g. undigits in
// the branch address. Blech. This means the branch ops
// must test the p_operands[0] value before accessing it.
//
if (branch || !p_fetch_fault) {
failed = (this->*opp->op_insn)(opp);
}

kushal bhattacharya

unread,
Jan 8, 2017, 7:00:55 AM1/8/17
to
thanks i am trying to avoid the old function pointer way of callback. I was looking for the latest c++ compliant callback function and the std::function to me suits my purpose now.
Thanks,
Kushal

0 new messages