rvalue-reference

Difference between “return-by-rvalue-ref” & “return-by-value” when you return using std::move?

我与影子孤独终老i 提交于 2019-11-29 04:08:51
Considering the following code: #include <iostream> using namespace std; struct I { I(I&& rv) { cout << "I::mvcotr" << endl; } }; struct C { I i; I&& foo() { return move(i) }; } }; int main() { C c; I i = c.foo(); } C contains I. And C::foo() allows you to move I out of C. What is the difference between the member function used above: I&& foo() { return move(i) }; // return rvalue ref and the following replacement member function: I foo() { return move(i) }; // return by value To me, they seem to do the same thing: I i = c.foo(); leads to a call to I::I(I&&); . What consequences will there be

Assigning this pointer to rvalue reference to a pointer

倖福魔咒の 提交于 2019-11-29 03:53:40
Should the following sample compile? struct B; struct A { A(B*&&){} }; struct B : A { B() : A(this){} }; int main(){} On LWS with clang it compiles, but with gcc I get: no known conversion for argument 1 from 'B* const' to 'B*&&' and if I add a const it compiles. I would like to also point out MSVC gets it wrong too: cannot convert parameter 2 from 'B *const ' to 'B *&&' so it looks like we have a bug in two compilers. BUGS FILED MSVC bug link GCC bug link GManNickG Yes, that should compile. It is incorrect to implement this as cv T* const (where cv is the cv-qualifiers for the function, if

Passing non-const references to rvalues in C++

孤者浪人 提交于 2019-11-29 03:42:21
In the following line of code: bootrec_reset(File(path, size, off), blksize); Calling a function with prototype: static void bootrec_reset(File &file, ssize_t blksize); I receive this error: libcpfs/mkfs.cc:99:53: error: invalid initialization of non-const reference of type 'File&' from an rvalue of type 'File' libcpfs/mkfs.cc:30:13: error: in passing argument 1 of 'void bootrec_reset(File&, ssize_t)' I'm aware that you can not pass non-const references ( const & ) to rvalues according to the standard. MSVC however allows you to do this (see this question ). This question attempts to explain

Understanding rvalue references

主宰稳场 提交于 2019-11-29 01:29:06
I think there's something I'm not quite understanding about rvalue references. Why does the following fail to compile (VS2012) with the error 'foo' : cannot convert parameter 1 from 'int' to 'int &&' ? void foo(int &&) {} void bar(int &&x) { foo(x); }; I would have assumed that the type int && would be preserved when passed from bar into foo. Why does it get transformed into int once inside the function body? I know the answer is to use std::forward : void bar(int &&x) { foo(std::forward<int>(x)); } so maybe I just don't have a clear grasp on why . (Also, why not std::move ?) I always remember

Are value parameters implicitly moved when returned by value?

点点圈 提交于 2019-11-29 01:11:31
Consider the following function: Foo foo(Foo x) { return x; } Will return x invoke the copy constructor or the move constructor? (Let's leave NRVO aside here.) To investigate, I wrote a simple Foo class that is only movable but not copyable: struct Foo { Foo() = default; Foo(const Foo&) = delete; Foo(Foo&&) = default; }; If the move constructor were invoked when returning value parameters by value, all should be fine. But the current g++ compiler complains about return x with the following error message: error: deleted function 'Foo::Foo(const Foo&)' If I replace return x with return std::move

What is an rvalue reference to function type?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 00:37:18
问题 I have recently wrapped my mind around the C++0x's concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there's one thing which still eludes me: What is "an rvalue reference to function type" ? It is literally mentioned many times in the drafts. Why was such a concept introduced? What are the uses for it? 回答1: I hate to be circular, but an rvalue reference to function type is an rvalue reference to function type. There is such a thing as a function type, e.g.

Does D have something akin to C++0x's move semantics?

血红的双手。 提交于 2019-11-28 22:42:22
A problem of "value types" with external resources (like std::vector<T> or std::string ) is that copying them tends to be quite expensive, and copies are created implicitly in various contexts, so this tends to be a performance concern. C++0x's answer to this problem is move semantics , which is conceptionally based on the idea of resource pilfering and technically powered by rvalue references . Does D have anything similar to move semantics or rvalue references? I believe that there are several places in D (such as returning structs) that D manages to make them moves whereas C++ would make

Should the assignment operator observe the assigned object's rvalueness?

随声附和 提交于 2019-11-28 21:28:59
For class types it is possible to assign to temporary objects which is actually not allowed for built-in types. Further, the assignment operator generated by default even yields an lvalue: int() = int(); // illegal: "expression is not assignable" struct B {}; B& b = B() = B(); // compiles OK: yields an lvalue! ... but is wrong! (see below) For the last statement the result of the assignment operator is actually used to initialize a non- const reference which will become stale immediately after the statement: the reference isn't bound to the temporary object directly (it can't as temporary

C++11 rvalue reference calling copy constructor too

旧巷老猫 提交于 2019-11-28 21:13:31
I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include <iostream> #include <vector> using namespace std; class TestClass{ public: TestClass(int s): size(s), arr(new int[s]){ } ~TestClass(){ if (arr) delete arr; } // copy constructor TestClass(const TestClass& other): size(other.size), arr(new int[other.size]){ std::copy(other.arr, other.arr + other.size, arr); } // move constructor TestClass(TestClass&& other){ arr=other.arr; size=other.size; other.arr=nullptr; other.size=0; }

Perfect forwarding for void and non-void returning functions

我的未来我决定 提交于 2019-11-28 20:52:11
Previously I was using a macro to measure the time a function call took whenever I wanted to quickly check that. Now, with C++11 available, I would like to finally remove that ugly peace of preprocessor code and replace it with something like this: template <typename Functor, typename ... Args> auto measure(Functor f, Args && ... args) -> decltype(f(std::forward<Args>(args)...)) { auto now = std::chrono::high_resolution_clock::now(); auto ret = f(std::forward<Args>(args)...); auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now() - now)