- Interesting little program - 6 Updates
- Compiler issues - 3 Updates
- Reversing a linked list iteratively - 5 Updates
- function 3 parameters, but called with 2 arguments - 1 Update
Doug Mika <dougmmika@gmail.com>: Jul 27 10:13AM -0700 I came upon this little program in a book. I like it, but I'm confused by one little thing: in the for_each(InputIterator first, InputIterator last, Function fn) construct, we basically call fn(*first). (where first is iterated) However, in the program bellow I don't see where the parameter (*first) would be: void do_work(unsigned id); void f() { std::vector<std::thread> threads; for(unsigned i=0;i<20;++i){ threads.push_back(std::thread(do_work,i)); } std::for_each(threads.begin(),threads.end(), std::mem_fn(&std::thread::join)); } that is: 1) std::mem_fn declares a functor(function object) where our function is &std::thread::join, but this function DOESN'T take a parameter, in particular, it doesn't take a parameter of type std::thread?? |
Victor Bazarov <v.bazarov@comcast.invalid>: Jul 27 01:21PM -0400 On 7/27/2015 1:13 PM, Doug Mika wrote: > } > that is: > 1) std::mem_fn declares a functor(function object) where our function is &std::thread::join, but this function DOESN'T take a parameter, in particular, it doesn't take a parameter of type std::thread?? 'mem_fn' is an adapter. It's a function (template) that returns an object of a class template instantiated with arguments deduced from the argument to the 'mem_fn' function. Take a look at how that class template's 'operator()' is implemented. I could write it down for you, but it's better if you find the actual implementation in your compiler headers and study it. V -- I do not respond to top-posted replies, please don't ask |
Doug Mika <dougmmika@gmail.com>: Jul 27 11:22AM -0700 the definition of mem_fn goes as follows : template <class Ret, class T> /* unspecified */ mem_fn (Ret T::* pm); Convert member function to function object Returns a function object whose functional call invokes the member function pointed by pm. The type of the returned object has the following properties: Its functional call takes as first argument an object of type T (or a reference or a pointer to it) and, as additional arguments, the arguments taken by pm (if any). The effect of such a call with fn as first argument are the same as calling fn.*pm (or (*fn).*pm if fn is a pointer), forwarding any additional arguments. If instead I call with fn2 as an argument instead of fn, must fn2 have a member function pm or simply satisfy that all conditions of *pm be met on the object fn2 (that all necessary member variables required by *pm exist in fn2)? |
Victor Bazarov <v.bazarov@comcast.invalid>: Jul 27 02:43PM -0400 On 7/27/2015 2:22 PM, Doug Mika wrote: > /* unspecified */ mem_fn (Ret T::* pm); > Convert member function to function object > Returns a function object whose functional call invokes the member function pointed by pm. So, what object does it return? What does its definition look like? How is *its* operator() implemented? Did you actually look, as I have suggested, in the headers that came with your compiler? > The type of the returned object has the following properties: > Its functional call takes as first argument an object of type T (or > a reference or a pointer to it) and, as additional arguments, the arguments taken by pm (if any). The effect of such a call with fn as first argument are the same as calling fn.*pm (or (*fn).*pm if fn is a pointer), forwarding any additional arguments. ? > have a member function pm or simply satisfy that all conditions of > *pm be met on the object fn2 (that all necessary member variables > required by *pm exist in fn2)? I have no idea what you're talking about. V -- I do not respond to top-posted replies, please don't ask |
Doug Mika <dougmmika@gmail.com>: Jul 27 11:47AM -0700 On Monday, July 27, 2015 at 1:43:26 PM UTC-5, Victor Bazarov wrote: > V > -- > I do not respond to top-posted replies, please don't ask Unfortunately, I currently don't have a working version of C++ installed on my machine, and I happen to use the cpp.sh/tutorialspoint compilers for my little academic challenges. |
red floyd <no.spam@its.invalid>: Jul 27 02:32PM -0700 On 7/27/2015 11:47 AM, Doug Mika wrote: >> -- >> I do not respond to top-posted replies, please don't ask > Unfortunately, I currently don't have a working version of C++ installed on my machine, and I happen to use the cpp.sh/tutorialspoint compilers for my little academic challenges. Victor is right that you should figure it out for yourself, but since you don't seem to have headers.. Given: class T { public: void f(); }; std::mem_fn(T::f) creates a functor object that has an operator() that looks roughly like this (PSEUDOCODE): std::mem_fn::operator()(T& t) { return t.f(); } |
Gordon Burditt <gordon@hammy.burditt.org>: Jul 26 07:32PM -0500 > Got a weird, maybe some gurus might be able to help me out. > By mistake I tpe gmke -j . Do you have a program called gmke? ("which gmke" might help here, although it may not deal with unconventional shell builtins). Is it by any chance a symbolic link to code_red_virus? > since then on one system, the C compiler has not beeing working properly. How can you tell? > Test errors, programmes not seeing the correct data, > socket not starting ... > What can I do to rectify this situation? Rebooting the system and/or cycling power on it might help. |
doctor@doctor.nl2k.ab.ca (The Doctor): Jul 27 04:24AM In article <t_GdnTfVpp604SjInZ2dnUU7-budnZ2d@posted.internetamerica>, >> socket not starting ... >> What can I do to rectify this situation? >Rebooting the system and/or cycling power on it might help. Power cycles, reboots nor resets have not helped. -- Member - Liberal International This is doctor@@nl2k.ab.ca Ici doctor@@nl2k.ab.ca God,Queen and country!Never Satan President Republic!Beware AntiChrist rising! http://www.fullyfollow.me/rootnl2k Look at Psalms 14 and 53 on Atheism Abuse a man unjustly, and you will make friends for him. -Edgar Watson Howe |
Keith Thompson <kst-u@mib.org>: Jul 27 11:47AM -0700 >>> What can I do to rectify this situation? >>Rebooting the system and/or cycling power on it might help. > Power cycles, reboots nor resets have not helped. You simply haven't provided enough information to guess what the cause or solution of the problem might be. Did you really type "gmke -j", or was that a typo for "gmake -j"? If the latter, it will execute the commands specified in your Makefile, possibly in parallel. Those commands could do quite literally anything, and you haven't given us a hint what the Makefile looks like. You've also been vague about what "the C compiler has not beeing working properly" means. You haven't even told us which C compiler you're using. In any case, this doesn't look like a question about the C language, which is what we discuss here -- and I don't know why you cross-posted to comp.lang.c++. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
Paul <pepstein5@gmail.com>: Jul 27 08:29AM -0700 I'm confused by the code below for reversing a linked list although it seems to work. What puzzles me is that changes to p affect rest. I don't know why this is so, because rest is a copy. For example, why does p->next->next = p; change rest? rest is an earlier copy of p->next. rest is not a reference. Thanks a lot for explaining it, Paul template<typename T> void reverseRecursive(node<T>*& p) { if (!p) return; node<T>* rest = p->next; if (!rest) return; reverseRecursive(rest); p->next->next = p; p->next = nullptr; p = rest; } |
David Bradshaw <user@foo.bar>: Jul 27 12:11PM -0400 > p->next = nullptr; > p = rest; > } Paul, You need to understand that rest is a pointer, defined by the following line: > node<T>* rest = p->next; After this line of code, rest is pointing to the same piece of data that p->next points to. In other words, you are not copying any data, but creating pointers or 'bookmarks' into the existing data structure. This is a reverse-in-place algorithm, which means you are not creating a copy of your data structure at all, just manipulating the pointers to achieve a reversal of its elements. David -- |
Christopher Pisz <nospam@notanaddress.com>: Jul 27 11:15AM -0500 On 7/27/2015 10:29 AM, Paul wrote: > p->next = nullptr; > p = rest; > } Draw it out on paper: ...P..R...// Second level recursion P..R......// First level recursion [1][2][3].// memory I am not clear on what change to p is puzzling you or what copy you are referring to. Nothing is copied anywhere, but pointers are set to point to locations other pointers are pointing to. If by copy, you mean the location the pointer is pointing to at that level in the recursive stack, it doesn't change, but the contents of the location do. -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jul 27 06:09PM +0100 On 27/07/2015 16:29, Paul wrote: > p->next = nullptr; > p = rest; > } Using recursion to do this is a BAD idea as it doesn't SCALE. Consider a container with 10000000 elements... /Flibble |
Paul <pepstein5@gmail.com>: Jul 27 11:35AM -0700 On Monday, July 27, 2015 at 5:15:23 PM UTC+1, Christopher Pisz wrote: > to locations other pointers are pointing to. If by copy, you mean the > location the pointer is pointing to at that level in the recursive > stack, it doesn't change, but the contents of the location do. to see or respond to any such messages > --- I get it now. Thanks a lot to both of you. Paul |
red floyd <no.spam@its.invalid>: Jul 27 09:16AM -0700 On 7/25/2015 7:21 PM, Melzzzzz wrote: >>> What happened? >> Are you sure there isn't a second, overloaded version? > Could be also default argument declared in header... Sounds like it, since he says "num_bytes is defined as 4 in a header file" |
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