rvalue-reference

What are “rvalue references for *this” for?

妖精的绣舞 提交于 2019-12-17 15:36:22
问题 What are the most typical use cases of "rvalue references for *this" which the standard also calls reference qualifiers for member functions? By the way, there is a really good explanation about this language feature here. 回答1: When called, each member function has an implicit object parameter that *this references. So (a) these normal function overloads: void f(const T&); void f(T&&); when called like f(x) ; and (b) these member function overloads: struct C { void f() const &; void f() &&; }

Avoid exponential grow of const references and rvalue references in constructor

青春壹個敷衍的年華 提交于 2019-12-17 15:28:44
问题 I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies for different functionalities, for example: template <class Loss, class Optimizer> class LinearClassifier { ... } The problem is with the constructors. As the amount of policies (template parameters) grows, the combinations of const references and rvalue references grow exponentially. In the

Can compiler generate std::move for a last use of lvalue automatically?

孤街浪徒 提交于 2019-12-17 06:54:13
问题 A code like this is often seen in r-value references articles: Dave Abrams: Move It With Rvalue References void g(X); void f() { X b; g(b); // still need the value of b … g( std::move(b) ); // all done with b now; grant permission to move } Could a compiler generate this optimization automatically, i.e. to detect a l-value is going to be destructed anyway and could be moved from, or would this be a violation of the standard, assuming a generic case the compiler does not know anything about

Can compiler generate std::move for a last use of lvalue automatically?

故事扮演 提交于 2019-12-17 06:54:11
问题 A code like this is often seen in r-value references articles: Dave Abrams: Move It With Rvalue References void g(X); void f() { X b; g(b); // still need the value of b … g( std::move(b) ); // all done with b now; grant permission to move } Could a compiler generate this optimization automatically, i.e. to detect a l-value is going to be destructed anyway and could be moved from, or would this be a violation of the standard, assuming a generic case the compiler does not know anything about

Can I typically/always use std::forward instead of std::move?

孤人 提交于 2019-12-17 06:24:08
问题 I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers says that he does not care about the answer because it is non-idiomatic and would silly his mind, but I'm still interested. The code presented is as follows: // Typical function bodies with overloading: void doWork(const Widget& param) // copy { //

How to actually implement the rule of five?

我的未来我决定 提交于 2019-12-17 05:40:49
问题 UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five items of the rule even needed? In practice, I'm starting something with 3D imaging where an image is usually 128*128*128 doubles. Being able though to write things like this would make the math alot easier: Data a = MakeData(); Data c = 5 * a + ( 1

How to actually implement the rule of five?

故事扮演 提交于 2019-12-17 05:40:35
问题 UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five items of the rule even needed? In practice, I'm starting something with 3D imaging where an image is usually 128*128*128 doubles. Being able though to write things like this would make the math alot easier: Data a = MakeData(); Data c = 5 * a + ( 1

Is it possible to std::move objects out of functions? (C++11)

瘦欲@ 提交于 2019-12-17 05:11:26
问题 This program tries to move a string out of a function and use it for the construction of another string: #include <iostream> #include <string> #include <utility> std::string && Get_String(void); int main(){ std::string str{Get_String()}; std::cout << str << std::endl; return 0; } std::string && Get_String(void){ std::string str{"hello world"}; return std::move(str); } The program compiles, but segfaults upon execution. This was my rationale: Get_String will create a local string. A copy of

non-class rvalues always have cv-unqualified types

淺唱寂寞╮ 提交于 2019-12-17 04:29:08
问题 §3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } According to the standard, there is no such thing as a const rvalue for non-class types, yet bar() prefers to bind to const int&& . Is

non-class rvalues always have cv-unqualified types

喜夏-厌秋 提交于 2019-12-17 04:29:02
问题 §3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } According to the standard, there is no such thing as a const rvalue for non-class types, yet bar() prefers to bind to const int&& . Is