- use of callback function in c++14 - 4 Updates
- use of callback function in c++14 - 2 Updates
- Problem with std::set - 1 Update
- The Longest Common Sequence ( NonContiguous ) algorithm ( recursive ). - 1 Update
kushal bhattacharya <bhattacharya.kushal4@gmail.com>: Jan 06 03:37AM -0800 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 <wouter@voti.nl>: Jan 06 12:57PM +0100 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" <alf.p.steinbach+usenet@gmail.com>: Jan 06 05:02PM +0100 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@slp53.sl.home (Scott Lurndal): Jan 06 05:34PM >* 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); } |
ram@zedat.fu-berlin.de (Stefan Ram): Jan 06 04:56PM >Is callbackback function used in current application >projects(in C++)? The C++ norm does not use the term »callbackback function«. But it /uses/ the term »callback« in 27.5.3.6. You might check out 27.5.3.6 void register_callback(event_callback fn, int index); as an inspiration. >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 In the code below, a lambda is being passed as a callback into »on_draw«. You can see »sp«'s use count being incremented by »bad« as the program first prints »1« and then »2«. #include <iostream> #include <ostream> #include <memory> #include <functional> #include <utility> struct object { void on_draw( std::function<void()> x ) { static std::function<void()> r = x; } }; struct X { void extra_work(){} }; void bad( const ::std::shared_ptr< X >& x ) { object obj; obj.on_draw( [=]{ x->extra_work(); }); } int main() { auto sp = ::std::make_shared< X >(); ::std::cout << sp.use_count() << '\n'; bad( sp ); ::std::cout << sp.use_count() << '\n'; } So, under some circumstance you might wish to provide a means to unregister a callback again, so as to avoid memory leaks, or only hand in weak pointers. |
ram@zedat.fu-berlin.de (Stefan Ram): Jan 06 05:03PM >void bad( const ::std::shared_ptr< X >& x ) >{ object obj; obj.on_draw( [=]{ x->extra_work(); }); } PS: Above, I wanted »obj« to have a larger lifetime. So please move it out of the block, mentally. But with this small lifetime, now there surely is a leak as the static variable »r« keeps the object of »sp« alive without any hope to ever change »r« again. |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jan 06 10:19AM On Thu, 2016-12-22, Joseph Hesse wrote: > pointer. > I compiled the code with: > $ g++ -std=c++11 Set.cpp You may want to add some or all of '-Wall -Wextra -pedantic' to that command line. > std::set<X, Comp> SetOfX; // <== use function pointer > return 0; > } It's not what you ask for, but I find the simplest thing is to, whenever possible: std::set<X> SetOfX; and define operator< for X. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Lofty Goat <rlwatkins@gmail.com>: Jan 05 05:43PM -0600 >When I see sparse code, with lots of whitespace and comments, >I _immediately know the programmer is struggling. No, you don't know that. That's an expression of desperate bravado on your part, not of experience or wisdom. I've been programming for 45 years. I've seen your code. You're a terrible programmer. Why do I say so? You play with language features like a tiny child randomly plugging tinkertoy parts together, rather than using them as they were intended, to construct useful, maintainable processes. You're pretty smart, but apparently lack the confidence to do anything that's both useful and complex. Stop fucking around and try it. You might succeed. What you're doing now is deliberate, and elaborate and therefore obscure, failure. -- Goat |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment