tag:blogger.com,1999:blog-6987514089363722750.comments2022-03-23T20:46:40.644-04:00[Un]defined behaviorDavid Rodríguezhttp://www.blogger.com/profile/02117106672577313085noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-6987514089363722750.post-5109247310453784912022-02-04T23:05:17.769-05:002022-02-04T23:05:17.769-05:00Merit Casino | Online Casino Review India
This cas...Merit Casino | Online Casino Review India<br />This casino offers online casino games and the best betting products in the world. All of the major <a href="https://xn--o80b910a26eepc81il5g.online/merit-casino/" rel="nofollow">메리트카지노</a> online casinos provide generous bonuses for newAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-87601633167320524482017-07-07T11:52:23.288-04:002017-07-07T11:52:23.288-04:00Thanks for the article very usefulThanks for the article very usefulVivek J Joshihttps://www.blogger.com/profile/14299961437345500554noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-84453850154231854552014-05-29T05:00:43.509-04:002014-05-29T05:00:43.509-04:00Hi David,
"But all the code in function uses ...Hi David,<br />"But all the code in function uses Y so it cannot be removed either, unless the object called Y inside function is located over the agreed location of the _R object."<br />I did not understand above paragraph. <br />thanks for very good article.<br /><br />Regards,<br />Vinayvinayschttps://www.blogger.com/profile/07105735353373664008noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-84138746752878067652014-01-17T18:41:43.861-05:002014-01-17T18:41:43.861-05:00Typo in your "operator+" example:
swap(...Typo in your "operator+" example:<br /><br />swap( tmp, rhs );<br /><br />...should read...<br /><br />swap ( tmp, lhs );<br /><br />The description in the subsequent text is correct.Nemohttps://self-evident.org/noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-53404451939368751692013-10-15T23:36:17.556-04:002013-10-15T23:36:17.556-04:00David Could you please find some time and continue...David Could you please find some time and continue your blogs? its really helpful to learn from you. Maybe when you answer a question at stack overflow you can extend those answers here(if possible) or pick out a new topic.This is a request since i can understand clearly when you explain. cheers:-). Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-11032151954822629872013-06-26T17:35:11.138-04:002013-06-26T17:35:11.138-04:00Long story... changed job, city, country, got marr...Long story... changed job, city, country, got married and have a kid. Writing takes time, and that is something I don't have much now. I have a couple of things I'd like to write about, lookup will require a number of entries but should be an interesting topic.David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-44195059127206360672013-06-26T15:52:39.581-04:002013-06-26T15:52:39.581-04:00Why is that you don't blog anymore? . I really...Why is that you don't blog anymore? . I really miss articles from your side :(Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-40857340421681242922013-01-09T08:32:56.192-05:002013-01-09T08:32:56.192-05:00Maybe the picture is not clear enough, so I will t...Maybe the picture is not clear enough, so I will try describing it. In a fully unoptimized compiler, inside <i>main</i>, the compiler reserves space for the variable <i>x</i> and also for the returned object from the call to <i>f()</i>. It then calls <i>f()</i> that will fill in <i>f::_R</i> in the picture. At this point there is a sequence point (all side effects of the call to <i>f()</i> (i.e. updating <i>f::_R</i>) complete. The compiler then creates space for the argument and returned objects in <i>g()</i>. It copies the value of <i>f::_R</i> into <i>arg</i> and calls <i>g()</i>.<br /><br />Regarding the "We cannot avoid having two objects in the program..." it is not just limited by the circumstances under which copy elision can take place. In C++11 the language explicitly states that the copy from the argument to the returned object cannot be elided, but C++03 did not have such wording.<br /><br />The core reason why the copy from the argument to the returned value cannot be elided is that C and C++ have a separate compilation model, which means that while processing the code that performs the call to the function, the implementation of the function need not be available. If the implementation is available, the compiler is able to inline and it can do anything (including using a single object in the program), but in general that is not the case. Assuming that the implementation is not available, the call to the function is done by following the calling conventions for the platform, and that calling convention determines how each argument is passed in and how the return value comes out, and this means that the compiler is not free to relocate the two in the same memory location.<br /><br />A common calling convention for returning large objects (larger than a register) has the caller reserve space for the return object and pass a pointer to that location as a hidden argument. The caller is also responsible for making the copy of the object for the argument. At this point it can be argued that the caller could make the copy for the argument in the stack and pass a pointer to that object, but that would require knowing that the returned object is the parameter. David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-46669404424124130912013-01-08T01:16:45.974-05:002013-01-08T01:16:45.974-05:00In picture "Before copy elision", I thin...In picture "Before copy elision", I think is:<br />x -> g::_R -> arg -> f::_R.<br />And I'm not clear about "We cannot avoid having two objects in the program...", Is that because the standard does restrict the circumstance to use copy elision?<br /><br />thanks.<br />walfudhttps://www.blogger.com/profile/00788152833548852685noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-50341640289300487472012-10-15T10:10:40.443-04:002012-10-15T10:10:40.443-04:00@Amirhossein: It does not need to know which value...@Amirhossein: It does not need to know which value it will use, but for simple small functions it can perform the transformation shown in the next block of code in the post: move the 'if' to be the first expression, then in each of the two branches which object 'x' or 'y' to be returned is fixed. <br /><br />Note that the compiler does not need to know beforehand which branch it will take. It only needs to know <i>when creating the variable</i> on which the copy elision will be taken.<br /><br />A common implementation of a function returning by value in the calling conventions is a function that returns nothing and takes a pointer to the location where the object will be created. Maybe looking at it that way helps you understand that particular case:<br /><br />// type function(bool which):<br />void __function(void *ret, bool which) {<br /> if (which) {<br /> new (ret) type; // x<br /> type y;<br /> return; // return x<br /> } else {<br /> type x;<br /> new (ret) type; // y<br /> return; // return y<br /> }<br />}<br /><br />(excuse the poor formatting as it does not seem to allow code formatting in comments)David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-20610287634908176862012-10-13T06:30:39.765-04:002012-10-13T06:30:39.765-04:00Thanks David for the interesting post. Question: i...Thanks David for the interesting post. Question: in the example you gave: <br /><br />type function( bool which ) {<br /> type x,y;<br /> if ( which ) {<br /> return x;<br /> } else {<br /> return y;<br /> }<br />}<br /><br />how does the compiler know about the fate of the 'if' statement, if 'which' is not a constant ?Amirhosseinhttps://www.blogger.com/profile/07056859947632782079noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-20026628273799254772012-02-01T03:50:08.844-05:002012-02-01T03:50:08.844-05:00awesome article, loved it :)awesome article, loved it :)Mr.Anubisnoreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-39020000210846780342012-01-13T16:01:01.800-05:002012-01-13T16:01:01.800-05:00Its a wonderful thing that people like you share y...Its a wonderful thing that people like you share your knowledge so selflessly and passionate about blogging things which can be useful to others ...Regardsriteshhttps://www.blogger.com/profile/18400663696232070706noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-29502826652078383722011-09-07T11:24:37.143-04:002011-09-07T11:24:37.143-04:00@Nawaz: Yes, that approach would also be correct (...@Nawaz: Yes, that approach would also be correct (and seeing the comment I just realized that the code was missing the return statement!). In your proposal, the comma-operator would be used to first execute the function and then evaluate the 0 as an expression, which would then be used to initialize the variable.<br /><br />I still prefer returning an integer on the principle of least surprise: most people will understand the integer return, but some will be puzzled by the use of the comma operator.<br /><br />Thanks for reading and taking the time to comment!David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-80468553395841557112011-09-07T06:55:55.006-04:002011-09-07T06:55:55.006-04:00Nice technique. Thanks for it.
By the way, the re...Nice technique. Thanks for it.<br /><br />By the way, the return type need not to be `int`. It could be `void` and you can still initialize the static variable `xxx_ignored` as:<br /><br />void f() {}<br />static const int xxx_ignored = (f(),0);<br /><br />which is fine, as far as I know. http://www.ideone.com/KFTvgNawazhttp://stackoverview.blogspot.com/noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-86373275219748233582011-08-26T05:08:49.316-04:002011-08-26T05:08:49.316-04:00Thanks Ben for bringing that up. I completely agre...Thanks Ben for bringing that up. I completely agree that code repetition should be avoided as much as possible, and that is a bit I just did not think of while writing the post.<br /><br />I have reworked the final version so that the stop condition does not have any associated code by reworking the indices (each specialization for N updates the element N-1, making the new specialization for N=0 equivalent to your proposed specialization for N=-1.David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-49402294989312989102011-08-25T17:05:34.508-04:002011-08-25T17:05:34.508-04:00I think, in the interest of avoiding code duplicat...I think, in the interest of avoiding code duplication, I would specialize N=-1 to do nothing, instead of N=0 to make an entry and not recurse. Altogether a very nice technique though.Ben Voigthttps://www.blogger.com/profile/02101435112895253110noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-43502150864900502202011-07-22T04:15:19.542-04:002011-07-22T04:15:19.542-04:00Thanks for the heads up with respect to the typo. ...Thanks for the heads up with respect to the typo. I am actually surprised that *anyone* has read this... I thought I had at least a few weeks where I could play with the tool and learn how to write, post, format code...David Rodríguezhttps://www.blogger.com/profile/02117106672577313085noreply@blogger.comtag:blogger.com,1999:blog-6987514089363722750.post-18092324564816861582011-07-21T21:30:21.339-04:002011-07-21T21:30:21.339-04:00third paragraph should read "why is std::pair...third paragraph should read "why is std::pair < A, A > implicitly convertible to std::pair < B, B > ?" - nice post though! (I messed up with the brackets)Anonymousnoreply@blogger.com