rvalue-reference

Can std::function be move-constructed from rvalue reference to a temporary functor object?

你。 提交于 2019-12-06 17:06:04
问题 I have an untemplated functor object that I'm trying to store as a std::function inside another object. This object is really heavyweight, so it's marked as uncopyable, but it does have a move constructor. However, trying to construct a std::function, or assign it, from a temporary constructor fails. Here is a minimal example to provoke the error. // pretend this is a really heavyweight functor that can't be copied. struct ExampleTest { int x; int operator()(void) const {return x*2;}

C++ move constructor not called for rvalue reference [duplicate]

半腔热情 提交于 2019-12-06 13:29:37
This question already has answers here : On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule (3 answers) Closed 3 years ago . class MyClass { public: MyClass() { std::cout << "default constructor\n"; } MyClass(MyClass& a) { std::cout << "copy constructor\n"; } MyClass(MyClass&& b) { std::cout << "move constructor\n"; } }; void test(MyClass&& temp) { MyClass a(MyClass{}); // calls MOVE constructor as expected MyClass b(temp); // calls COPY constructor... not expected...? } int main() { test(MyClass{}); return 0; } For the above code, I expected both object creations in test

C++11 - Distinguishing rvalue pointers

橙三吉。 提交于 2019-12-06 13:05:15
How can I distinguish a variable as a compiler-constructed string? For example, while the rvalue "Hello, World" is of type const char* . const char* in itself does not mean that a pointer can't be changed. A char* const pointer can't be changed, but that's not what's constructed by the compiler. Does this mean that, for any container that holds a const char* , the data should be copied by means other than C++'s move semantics? Is there any way to just move compiler-constructed strings and leave all other strings alone? For example, in the GCC 4.5.2, a method that returns the type int as

Why reference can not capture temporary while const ref and rval ref can [duplicate]

无人久伴 提交于 2019-12-06 11:53:41
This question already has answers here : How come a non-const reference cannot bind to a temporary object? (11 answers) Closed 5 years ago . Why reference can not capture temporary value while const reference and rvalue reference can capture and prolong object life. In other words while two first lines are legal but third not: const string &a = string("a"); string &&b = string("b"); string &c = string("c"); // why illegal? Quoting from N1377 Bjarne in his excellent text "The Design and Evolution of C++" discusses the motivation for prohibiting the binding of an rvalue to a non-const reference

rvalue reference with assignement operator

我们两清 提交于 2019-12-06 10:21:43
问题 In this article http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1877 : T& T::operator=(T const& x) // x is a reference to the source { T tmp(x); // copy construction of tmp does the hard work swap(*this, tmp); // trade our resources for tmp's return *this; // our (old) resources get destroyed with tmp } but in light of copy elision, that formulation is glaringly inefficient! It’s now “obvious” that the correct way to write a copy-and-swap assignment is: T&

invalid initialization of non-const reference from an rvalue

三世轮回 提交于 2019-12-06 08:41:30
问题 So I have the following function: void scan(std::istream& is, Handler& h); I want to call it in different ways, like: scan(std::cin, Handler()); scan(std::ifstream("myfile"), myhandler); The compiler complains about std::ifstream("myfile") and Handler() of being rvalues being passed as non-const references, so the complaint is legitimate, but what can I do? Neither function parameters cannot be const ( istream is modified while read and the handler changes its state during callbacks). If I

C++11 Move semantics behaviour specific questions

↘锁芯ラ 提交于 2019-12-06 06:50:40
I have read the below post which gives a very good insight into move semantics: Can someone please explain move semantics to me? but I am still fail to understand following things regarding move semantics - Does copy elision and RVO would still work for classes without move constructors? Even if our classes doesn't have move constructors, but STL containers has one. For operation like std::vector<MyClass> vt = CreateMyClassVector(); and to perform operations like sorting etc. Why can't STL internally leverage move semantics to improve such operations internally using operations like copy

Perfect forwarding with multiple passes over input arguments

我的梦境 提交于 2019-12-06 05:43:39
问题 Consider the following function accept that takes a "universal reference" of type T and forwards that to a parse<T>() function object with an overload for lvalues and one for rvalues: template<class T> void accept(T&& arg) { parse<T>()(std::forward<T>(arg), 0); // copy or move, depending on rvaluedness of arg } template<class T> class parse { // parse will modify a local copy or move of its input parameter void operator()(T const& arg, int n) const { /* optimized for lvalues */ } void

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:32:20
I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference, then potentially many modifications could be done that would eventually be discarded (since an rvalue is

minmax that returns modifiable references

六眼飞鱼酱① 提交于 2019-12-05 22:11:29
With all the new features of C++ (C++11 suffices I think), what prevents from having a std::minmax function that returns a pair of references. In this way, if one feeds two modifable references, they can be modified. Is this opening a can of worms? #include<functional> // maybe all this options can be simplified template<class T1, class T2> struct common; template<class T> struct common<T, T>{using type = T;}; template<class T> struct common<T const&, T&>{using type = T const&;}; template<class T> struct common<T&, T const&>{using type = T const&;}; template<class T> struct common<T, T&>{using