tag:blogger.com,1999:blog-7101933101966798446.post1728363151202444936..comments2024-03-17T08:14:57.577-07:00Comments on The View from Aristeia: On the Superfluousness of std::moveScott Meyershttp://www.blogger.com/profile/05280964633768289328noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-7101933101966798446.post-37869346535793769522012-12-12T06:00:04.716-08:002012-12-12T06:00:04.716-08:00Thank you very much Mr.Scott Meyers.Thank you very much Mr.Scott Meyers.Mohammadhttps://www.blogger.com/profile/06170819139129265285noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-87004723341400780842012-12-10T09:07:14.863-08:002012-12-10T09:07:14.863-08:00@Anders Sjögren: This is an interesting question, ...@Anders Sjögren: This is an interesting question, and I don't know if there is a technical justification for the restriction. It's perhaps worth noting that some lvalues are already implicitly treated as rvalues, notably variables thrown as exceptions and variables used as return expressions, so there are already parts of the language where the behavior you advocate takes place. I encourage you to post this question to StackOverflow. Howard Hinnant is active there, so you'd have a good chance to get an answer from the guy who is largely responsible for the design of rvalue references.<br /><br />ScottScott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-46841922985445643622012-12-10T00:09:36.867-08:002012-12-10T00:09:36.867-08:00My understanding is that std::move and std::forwar...My understanding is that std::move and std::forward typically explicitly express the last use of a variable in a function, where move is used if the variable is created or taken by value in the function, where forward if it is taken as universal reference.<br /><br />Since the compiler can infer the last use and the origin of the variable, why not let the compiler entirely automate the move/forward business? In other words: What prevents the compiler from figuring out the last use of each variable and automatically inserting the equivalent of move and forward at that location?Anonymoushttps://www.blogger.com/profile/13217172115386951012noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-16275803093321019662012-12-09T06:51:38.268-08:002012-12-09T06:51:38.268-08:00It seems that blogger ate all my square brackets.....It seems that blogger ate all my square brackets... sorry for that! This is what I wrote<br /><br />Widget::Widget(Widget&& rhs)<br />: s(std::forward < std::string > (rhs.s))<br />{}<br /><br />If you leave the spaces between the brackets the type just disappears, reminds me of c++03 and >> :DAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-81876205841630452362012-12-07T21:08:07.230-08:002012-12-07T21:08:07.230-08:00@Mohammad: First, I apologize for the long delay. ...@Mohammad: First, I apologize for the long delay. I overlooked your comment.<br /><br />As for your question itself, I agree that having Widget inherit from NewHandlerSupport<Widget> does not correspond to the classic notion of "is-a," but Item 32 remarks that the use of public inheritance asserts that, given a base class B and a publicly derived class D, "anywhere an object of type B can be used, an object of type D can be used just as well, because every object of type D <i>is</i> an<br />object of type B." In this case, it's true that a Widget can be used anywhere a NewHandlerSupport<Widget> can, so the "is-a" requirement is satisfied. (The technical requirement is <i>substitutability</i>: publicly derived classes must be substitutable for base classes. In Item 49, that's the case: you can substitute a Widget for a NewHandlerSupport<Widget>, and your code will continue to work.)Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-358217552622996882012-12-05T15:03:19.409-08:002012-12-05T15:03:19.409-08:00@Anonymous: You're correct that rhs is an lval...@Anonymous: You're correct that rhs is an lvalue, and you'd be right that std::forward would return an lvalue reference if called with rhs.s, but you have to pass std::forward a type argument, so the code you wrote would not compile. <br /><br />ScottScott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-88900844724369818652012-12-05T14:14:48.278-08:002012-12-05T14:14:48.278-08:00Hi Scott! I love your talks and articles! I have o...Hi Scott! I love your talks and articles! I have one question. Say some function returns a temporary Widget and the move constructor of Widget is called:<br /><br />Widget::Widget(Widget&& rhs)<br />: s(std::forward(rhs.s)<br />{}<br /><br />As I understood it, inside the move constructor, rhs has a name and I can take its address as well as the address of rhs.s, so I would expect it to be an lvalue reference. That is, I would expect std::forward to do the right thing and return an lvalue reference too. I thought the point of using std::move(rhs.s) there was to force the call to the move constructor of string, something that std::forward would not do. <br /><br />I guess I got it completely wrong :sAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-89027246267515302932012-12-01T08:34:01.055-08:002012-12-01T08:34:01.055-08:00Hello Mr. Scott Meyers
I encounter a problem in re...Hello Mr. Scott Meyers<br />I encounter a problem in reading your book. Would you please help me!<br />I don't know where should i ask it so I post it in StackOwerflow.<br />Thank you!<br /><br />http://stackoverflow.com/questions/13660451/public-inheritance-and-mixin-styleMohammadhttps://www.blogger.com/profile/06170819139129265285noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-90927046640783472532012-11-22T11:16:50.847-08:002012-11-22T11:16:50.847-08:00std::forward<T&&>(t) is exactly the ...std::forward<T&&>(t) is exactly the same as std::forward<T>(t), whether or not T is an lvalue reference. The only difference is that the former is syntactically noisier.Anonymoushttps://www.blogger.com/profile/03002580591828566921noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-80005481278008443332012-11-16T11:03:07.475-08:002012-11-16T11:03:07.475-08:00@Anonymous: The reason why std::forward<string&...@Anonymous: The reason why std::forward<string&&> is strange is that the template argument for std::forward is expected to be a deduced parameter type, and deduced parameter types are lvalue references for lvalues and non-references for rvalues. Because std::string&& is neither of these, it looks odd. I can't think of any way in which it would behave differently from std::string (i.e., a non-reference) inside std::forward, so I think the net effect of passing std::string&& would be the same as passing std::string, but I'm not sure, and it certainly runs contrary to convention.<br /><br />BTW, the rvalue-ref stuff is still sinking in for me, too, and I've been trying to figure it out for about three years :-)Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-13128267231391434022012-11-16T10:48:38.734-08:002012-11-16T10:48:38.734-08:00Can you explain (for someone who's not very br...Can you explain (for someone who's not very bright) why writing <br /><br><br /><br><br />std::forward<std::string&&>(rhs.s)<br /><br><br /><br><br />would be reason to get kicked out of the C++ club? I feel like it's going to be a while before all this rvalue-ref stuff really sinks in for me.<br /><br />Oh, and there's still one missing paren (in the example just following the "Per convention..." line).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-14865979257700785202012-11-14T07:21:08.280-08:002012-11-14T07:21:08.280-08:00move() and forward<>() may look similar at a...move() and forward<>() may look similar at a low level, but semantically are really completely separate ideas. You might be able to implement one in terms of the other, but there's a very good reason that the standard provides both.<br /><br />You use move(x) when you <i>know</i> you want to move from x, because it is a value that is no longer needed for anything else. Think of this, at a high level, as enabling an optimization of copy.<br /><br />You use forward(x) when you don't know whether x is bound to an rvalue or lvalue in your caller, so its value might be needed even after you return. Think of this, at a high-level, as ressurecting any rvalue-ness that may have been erased by giving the argument a name.Dave Abrahamshttps://www.blogger.com/profile/11169343557510903330noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-4059732211866885752012-11-12T09:54:18.498-08:002012-11-12T09:54:18.498-08:00@Leszek Swirski: As I wrote in the article, std::m...@Leszek Swirski: As I wrote in the article, std::move is an unconditional cast to an rvalue, so it takes steps to ensure that reference collapsing does not yield an lvalue reference as a a return type. I would not say that std::move is a special case of std::forward, because they are designed to be called using different syntax and to accomplish different things. There are various ways to implement these functions, and I suppose that std::move could even be implemented in terms of std::forward, but I've never heard of this being done.Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-40478702602403713062012-11-12T09:48:47.840-08:002012-11-12T09:48:47.840-08:00As I understand it, std::move is a special case of...As I understand it, std::move is a special case of std::forward, which passes std::remove_reference::type&& as the type parameter of T to force it to be an rvalue reference, rather than relying on the reference collapsing rules (the "scary truth") to make the right choice. Is this right?<br /><br />Btw, if you want a more minor bug report, one tends to just say "anathema" as opposed to "an anathema" <a href="http://www.fluther.com/38381/when-do-you-use-anathema-vs-an-anathema/" rel="nofollow">[Source]</a>.Leszek Swirskihttps://www.blogger.com/profile/13990465834437914379noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-1288774801988980712012-11-12T09:28:14.786-08:002012-11-12T09:28:14.786-08:00@Anonymous and @Nacho: Thanks for the typo bug rep...@Anonymous and @Nacho: Thanks for the typo bug reports. Both are now fixed.Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-49414464596596557872012-11-12T09:27:41.862-08:002012-11-12T09:27:41.862-08:00@Zahir: As a general rule, the standardization com...@Zahir: As a general rule, the standardization committee prefers to implement features in the library instead of in the core language. I assume that because std::move and std::forward can be implemented as library functions, the committee saw no reason to modify the language proper. Scott Meyershttps://www.blogger.com/profile/05280964633768289328noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-49498400122519278172012-11-12T07:25:26.380-08:002012-11-12T07:25:26.380-08:00I'm the asker of the question on StackOverflow...I'm the asker of the question on StackOverflow. Thanks for the response, Scott!Joseph Mansfieldhttps://www.blogger.com/profile/09285504490640213417noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-49880041903207825722012-11-12T02:40:48.106-08:002012-11-12T02:40:48.106-08:00You have a typo on the second code paragraph. Now ...You have a typo on the second code paragraph. Now guess....<br /><br />No, really, it's the second return.Nachohttps://www.blogger.com/profile/07057166065724555515noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-25843568396240869832012-11-12T02:21:32.658-08:002012-11-12T02:21:32.658-08:00The last code sample is missing a close paren for ...The last code sample is missing a close paren for the initializer.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-50812094894675933142012-11-12T00:00:35.106-08:002012-11-12T00:00:35.106-08:00Seinfeld (seaon 2, episode 6)
George: Students ca...Seinfeld (seaon 2, episode 6)<br /><br />George: Students can't clean. It's anathema. (explaining) They don't like it.<br /><br />Jerry: How long have you been waiting to squeeze that into a conversation?<br /><br />Rein Halbersmahttps://www.blogger.com/profile/09512938204991225090noreply@blogger.comtag:blogger.com,1999:blog-7101933101966798446.post-19585652960040604222012-11-11T23:12:47.107-08:002012-11-11T23:12:47.107-08:00Do you have any idea about why these functions are...Do you have any idea about why these functions are 'functions' but not language keywords or a keyword like 'sizeof(param)'? I'd like it working without type deduction and only using the type on the declaration (without reference ofcourse) for which we should use forward for, otherwise it seems like a bug breeder.Zahirhttps://www.blogger.com/profile/04915673855138700742noreply@blogger.com