- Function Objects - 3 Updates
- In need of reading material - 4 Updates
- Strongly typed enum question - 3 Updates
- A C++ program - 3 Updates
- Onwards and upwards - 5 Updates
- A curiosity with a small program - 7 Updates
Paavo Helde <myfirstname@osa.pri.ee>: Apr 30 01:32PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > Human someHoman; > NOT > Human someHuman(); Because for the compiler this would look like a function declaration. Yes, it's not fully consistent, that's one of the reasons the new brace initialization syntax was introduced in C++11 so now you can write: Human someHuman{}; for defining a variable. |
legalize+jeeves@mail.xmission.com (Richard): Apr 30 07:03PM [Please do not mail me a copy of your followup] Doug Mika <dougmmika@gmail.com> spake the secret code >object using: >DisplayElement<int>() >am I declaring it on the stack or the heap? And when and by whom is it deleted? It's on the stack and its lifetime is that of the enclosing expression, namely the function call where this anonymous instance is supplied as an actual argument. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
"Öö Tiib" <ootiib@hot.ee>: Apr 30 04:02PM -0700 On Friday, 1 May 2015 01:10:05 UTC+3, Stefan Ram wrote: > >scope. > IIRC, this is valid for /const/ lvalue references and for > rvalue references, but not for /non-const/ lvalue references. Are these really called "lvalue references" now? It is impossible to initialize such a reference to non-const with temporary unless you use Microsoft Visual C or other non-standard and it seems that at least in visual c it also affects life-time of temporary. |
Doug Mika <dougmmika@gmail.com>: Apr 30 02:15PM -0700 Hi to all I have read much about passing parameters and objects by value and reference, and I think I understand all these concepts. The thing that is providing me with confusion is passing "functions" as parameters, be it by reference or by value. My books don't cover this and I don't quite know what phrase to search to find info on this. Does anyone know of any sites on the net that cover how functions are passed as parameters? After all they don't exist the same way in memory, or do they? Do we have such a thing as instances of functions? Thanks Doug |
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 30 05:29PM -0400 On 4/30/2015 5:15 PM, Doug Mika wrote: > I have read much about passing parameters and objects by value and reference, and I think I understand all these concepts. The thing that is providing me with confusion is passing "functions" as parameters, be it by reference or by value. My books don't cover this and I don't quite know what phrase to search to find info on this. Does anyone know of any sites on the net that cover how functions are passed as parameters? I don't, sorry. After all they don't exist the same way in memory, or do they? Do we have such a thing as instances of functions? You can think that functions are passed around as pointers, and functors (objects of classes that define their own op() member function) are passed around like all other objects. Look at a pointer to the function as the address in the computer memory of the first machine instruction into which that function code is translated. In C++ functions (pointers to them) also have types, so you can't pass a function with one set of argument types where a function with a different set is expected, but there are sometimes conversions. Perhaps "Inside the C++ Object Model" by Lippman contains more on that and other topics. Check it out. V -- I do not respond to top-posted replies, please don't ask |
Paavo Helde <myfirstname@osa.pri.ee>: Apr 30 05:13PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > and I don't quite know what phrase to search to find info on this. > Does anyone know of any sites on the net that cover how functions are > passed as parameters? Not really. The syntactis rules are quite confusing and any tutorial attempting to cover them would be confusing as well. The good thing is that you rarely need passing functions in C++ (virtual functions and functors are better for many usage cases) and when you need them you can use only a relatively small clear subset of the syntax - function pointers. See, in any case what is actually passed around is a pointer to the function, not the function itself, whatever that would mean. Effectively, functions are never passed "by value". C++ is not LISP. So one needs to deal just with function pointers, and these are small simple objects similar to data pointers. Pointers are naturally passed by value, and the pointed functions themselves stay whereever they are. > After all they don't exist the same way in > memory, or do they? Do we have such a thing as instances of > functions? No. There is something similar - instantiation of templates, which can produce multiple functions, but this happens at compile time and at runtime these would be all different unique nonchanging and non-moving functions (*). (*) In an ideal world. In practice I have had a bug where a static library was linked into different dynamic libraries so there were multiple instances of the same functions present in the process. This was all fine until a memory corruption bug ruined one of those copies. The result was that when a certain function was called via one dll everything worked fine, but if the same function with the same arguments was called through another dll all hell broke loose. So yes, in practice one can have multiple different "instances" of a function. But this would be a bug, not a feature. Cheers Paavo |
legalize+jeeves@mail.xmission.com (Richard): Apr 30 10:47PM [Please do not mail me a copy of your followup] Doug Mika <dougmmika@gmail.com> spake the secret code >reference, and I think I understand all these concepts. The thing that >is providing me with confusion is passing "functions" as parameters, be >it by reference or by value. When you pass the name of a function as a parameter, it is always an implicit pointer to the function and takes the same amount of space on the argument stack as any other pointer. #include <iostream> void f() { std::cout << "Hi!\n"; } void g(void fn()) { fn(); } int main() { g(f); } prints "Hi!\n" when you run it. The names of functions always implicitly decay to a pointer to the function. There isn't anything other you can do with the name of a function as a value other than take its address, either implicitly or explicitly. The syntax gets a little uglier for pointers to member functions, but the idea is the same -- you can only take the address of a member function and pass it as a pointer or a reference. A reference in C++ is mechanically equivalent to a pointer but has one important semantic difference -- a reference can never be NULL, so you can use references without checking whether or not they refer to something (unlike a pointer). If you're talking about "function objects" such as the things passed to the C++ standard library algorithms like "for_each", those are either plain old function pointers as discussed above, or they are instances of classes that implement operator(). In the case of passing function objects (also called functors), they are passed just like any other object. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 30 09:52PM >Unfortunately to you C++ language has already given a meaning >to 1. It is integer constant ^ an It's called »integer constant« in C (»6.4.4.1 Integer constants«), but »integer literal« in C++ (2.13.2 Integer literals). The expression »unfortunately for you« is much more common than »unfortunately to you«. »C++ language« without »the« sounds odd. Just look at the famous book by Bjarne Stroustrup. It's not called »C++ language«, it's called »The C++ programming language«. So I'd write this as (also notice the comma) »Unfortunately for you, the C++ language has already given a meaning to 1. It is an integer constant« or, with slightly more emphasis on »already«: »Unfortunately for you, the C++ language already has given a meaning to 1. It's an integer constant« However, being an integer literal is /not/ the meaning of the integer literal 1. It is only /a part/ of its meaning, it is one of its (lexical) categories (one of its non-terminal symbols). However, its meaning comprises more parts: it has the type »int«, the value »one«, and so one. (However, this would still not be quite correct, when one is looking at the »1« in isolation! Because in this case, it also might appear in a C program as part of identifier123 "string123" 123 /*comment123*/ ... , but this was not the way it was used in the OP.) |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 30 10:09PM >C++ standard does say "automatic storage" (that is usually stack) and >"dynamic storage" (that is usually heap). It always says »automatic storage duration« and »dynamic storage duration«. The difference between »automatic storage« and »automatic storage duration« is just like the difference between a »new bus« and a »new bus driver« (or a lightning and a lightning bug). >initialize reference variable with it. If you initialize reference >variable with it then it will be destroyed when that variable leaves >scope. IIRC, this is valid for /const/ lvalue references and for rvalue references, but not for /non-const/ lvalue references. |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 30 10:23PM >use only a relatively small clear subset of the syntax - function >pointers. »Function pointers« belong to the runtime model. They are values (or objects). The syntax describes C++ programs as source text, it's part of the source code model. Therefore, values and objects cannot be a »subset« of the syntax. You might actually think of »function pointer expressions«. But these also are not a /subset/ of the syntax, but rather a /part of a source code/ (which in turn is being described by the syntax). The syntax, to me (and some others), is the set of all terminal representations of the start symbol of the C++ grammar, that is, the set of all valid programs. Were function pointers a subset of the syntax, they would be a set of programs. |
legalize+jeeves@mail.xmission.com (Richard): Apr 30 06:59PM [Please do not mail me a copy of your followup] Paavo Helde <myfirstname@osa.pri.ee> spake the secret code >> haven't yet. >IMO you have not lost anything. My list of complaints about C::B is so long >I cannot even start. I heard it had some refactoring support, so I was going to run my refactoring test suite against it: <https://github.com/LegalizeAdulthood/refactor-test-suite> So far, CLion has been the best performer against this test suite. (Which reminds me, I need to rerun the suite against the CLion 1.0 release build.) -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
Ian Collins <ian-news@hotmail.com>: May 01 07:22AM +1200 嘱 Tiib wrote: >> haven't yet. > From general purpose IDEs NetBeans understands and highlights C++ lot > better than Code Blocks and tools seem also easier to integrate to it. NetBeans remote build options and lack of visual clutter are some of the reasons I prefer it over Eclipse. When developing for multiple compilers/targets "build all" from the IDE is a blessing! -- Ian Collins |
David Brown <david.brown@hesbynett.no>: Apr 30 10:56PM +0200 On 30/04/15 21:22, Ian Collins wrote: > NetBeans remote build options and lack of visual clutter are some of the > reasons I prefer it over Eclipse. When developing for multiple > compilers/targets "build all" from the IDE is a blessing! My builds are always done with external Makefiles, rather than the IDE's - I prefer to keep my source code and build system independent of the editor in use. Maybe I'll give NetBeans a shot some time - I've only heard good things about it. But Eclipse is the standard in the embedded world, and almost all integrated development tools are based on it (except Microchip uses NetBeans, and Atmel uses MSVC). It's easy enough to use a different editor, and Makefiles keeps the builds independent of the IDE, but sometimes low-level debugging can't be done with anything other than the vendor's tools - and that usually means Eclipse. Eclipse used to be painfully slow, buggy and bloated, but I find it works well these days. |
Daniel <danielaparker@gmail.com>: Apr 30 04:25AM -0700 On Wednesday, April 29, 2015 at 5:12:05 PM UTC-4, Chris Vine wrote: > > Leigh, please don't swear here. > I wish you could get it into your somewhat curious system of reasoning > that "fuck" is not swearing. Where I grew up, it was used as punctuation. Daniel |
woodbrian77@gmail.com: Apr 30 11:38AM -0700 > http://en.cppreference.com/w/cpp/string/basic_string/basic_string > It seems like that would help avoid some allocations and > moving of strings. I've done some performance testing now comparing my original ctor: explicit failure (char const* w) : whatStr(w) {} with this one: explicit failure (char const* w, int tot) { if (tot > 0) whatStr.reserve(tot); whatStr= w; } I used a test program like this: #include "ErrorWords.hh" void throws () { throw cmw::failure("In the beginning was the Word.", 41) << 33 << " abcdefg"; } int main () { int p = 0; for (int i = 0; i < 100000; ++i) { try { throws(); } catch (std::exception const& ex){ ++p; } } printf("p is %d\n", p); return 1; } -------------------------------------------------- The original version was about 20% slower than the newer version on PC-BSD 10.1 and clang 3.4.1. I believe the new ctor that I proposed could do a little better than the version that first reserves and then copy-assigns the string. And it would be easier to use as you wouldn't have to count/include the length of the original text that you pass to the ctor. I hope this ctor will be added to C++ 2015. Brian Ebenezer Enterprises - "What do you have that you have not received?" 1st Corinthians 4:7 http://webEbenezer.net |
woodbrian77@gmail.com: Apr 30 11:41AM -0700 On Thursday, April 30, 2015 at 6:25:44 AM UTC-5, Daniel wrote: > Where I grew up, it was used as punctuation. I'm sorry to hear that. |
Dombo <dombo@disposable.invalid>: Apr 30 08:45PM +0200 Op 29-Apr-15 23:11, Chris Vine schreef: > that "fuck" is not swearing. It is bad language (for some of the more > fastidious at least, personally I couldn't care a fuck). > Swearing is an offence to your god. Bad language is an offence to man. Fuck god then. |
Ian Collins <ian-news@hotmail.com>: May 01 07:15AM +1200 > that first reserves and then copy-assigns the string. And it > would be easier to use as you wouldn't have to count/include > the length of the original text that you pass to the ctor. I think you are missing the point - throwing an exception is an exceptional event and the cost of constructing an exception object is seldom a concern given all of the other overheads involved. -- Ian Collins |
Christian Gollwitzer <auriocus@gmx.de>: Apr 30 08:28PM +0200 Am 30.04.15 um 19:49 schrieb Richard: > the signature of for_each which takes the functor by value. To get > what you want, you have to use a referring wrapper like boost::ref, or > have your functor reference the count held elsewhere: ...or better yet, forget about for_each - this was just an auxiliary construct in C++ before 11, to avoid ugly for loops. Nowadays we have for (auto i: vecIntegers) { Result(i) } which does what you want, is more readable in my opinion and can also be done without a functor, if the only reason for the functor was for_each in the end. Christian |
Doug Mika <dougmmika@gmail.com>: Apr 30 11:29AM -0700 I guess one more newbie question: When this functor is passed by value, I assume it's the entire object (along with implementation of operator()) that is passed by value, and that if I have a copy constructor implemented, it will be used to pass that entire functor by value, correct? Thanks |
Paavo Helde <myfirstname@osa.pri.ee>: Apr 30 01:36PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > for_each(vecIntegers.begin(),vecIntegers.end(),Result); > the result of Result.Count is 0!!!, WHY isn't it 10????? Afterall, I > call the oprator() 10 times! Change this line to: Result = for_each(vecIntegers.begin(),vecIntegers.end(),Result); Victor already explained why your version did not work. |
Paavo Helde <myfirstname@osa.pri.ee>: Apr 30 01:51PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > I guess one more newbie question: > When this functor is passed by value, I assume it's the entire object > (along with implementation of operator()) An object does not contain any compiled machine code, so implementations of any functions or operators are not copied. Also, as the compiled code does not change during the run, it would be the most silly thing to copy it around in zillions of exemplars. > that is passed by value, and > that if I have a copy constructor implemented, it will be used to pass > that entire functor by value, correct? Yes, copy constructor will be used in the process (unless the compiler is able to optimize it away, but this probably does not happen here). |
legalize+jeeves@mail.xmission.com (Richard): Apr 30 07:01PM [Please do not mail me a copy of your followup] Doug Mika <dougmmika@gmail.com> spake the secret code >(along with implementation of operator()) that is passed by value, and >that if I have a copy constructor implemented, it will be used to pass >that entire functor by value, correct? Correct. If you don't implement the copy constructor, the compiler supplies one that does simple member-by-member assignment. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
legalize+jeeves@mail.xmission.com (Richard): Apr 30 07:02PM [Please do not mail me a copy of your followup] Paavo Helde <myfirstname@osa.pri.ee> spake the secret code >> call the oprator() 10 times! >Change this line to: >Result = for_each(vecIntegers.begin(),vecIntegers.end(),Result); Nice! I usually forget about the return value of for_each. Honestly though, range-based for loops in C++11 I don't expect I'll be using for_each much, even with lambdas. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 30 03:12PM -0400 On 4/30/2015 3:01 PM, Richard wrote: >> that entire functor by value, correct? > Correct. If you don't implement the copy constructor, the compiler > supplies one that does simple member-by-member assignment. Nit-pick: ".. that does simple member-by-member copy construction." V -- I do not respond to top-posted replies, please don't ask |
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. |