Move Semantics & Temporary Objects with Rvalue References

I just started reading The C++ Standard Library. It introduces move semantics and instructs you to get introduce to the topic elsewhere. After reading some threads online I kind of get the big picture but referencing:
http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
I have some questions.

In the code shown below, it states:
"it should be possible to skip the whole copy and just pilfer the pointer inside the temporary vector and keep it in v. In effect, why can't we move the object?"
I thought as soon as a function finishes executing it destroys all temporary values. This seems very similar in returning a reference to a temporary value. I know I'm missing something here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
 
using namespace std;
 
vector<int> doubleValues (const vector<int>& v)
{
    vector<int> new_values;
    new_values.reserve();
    for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr; ++itr )
    {
        new_values.push_back( 2 * *itr );
    }
    return new_values;
}
 
int main()
{
    vector<int> v;
    for ( int i = 0; i < 100; i++ )
    {
        v.push_back( i );
    }
    v = doubleValues( v );
}


Another question I have concerns the two lines of code below.

The author states:
"The intuition here is that you cannot use a "mutable" reference because, if you did, you'd be able to modify some object that is about to disappear, and that would be dangerous."

Mutable or not references to any temporary object seem dangerous. Why is it only dangerous to mutable references. If an object is available why isn't it okay to modify it? If it's because it might disappear why is it okay to reference it with a constant value.

Author states:
"Notice, by the way, that holding on to a const reference to a temporary object ensures that the temporary object isn't immediately destructed. This is a nice guarantee of C++, but it is still a temporary object, so you don't want to modify it."

Does C++ have something built-in that doesn't allow temporary values to be destructed it's referenced by a constant value. If so, why only constants? If it's still a temporary value, when does it disappear?

1
2
const string& name = getName(); // ok
string& name = getName(); // NOT ok 



Now I have a question about learning the Standard Library.
I'm reading The C++ Standard Library 2nd Edition, as I mentioned.
I'm on the third chapter and I'm so far disappointed. It uses examples to explain topics by using parts of the standard library that hasn't been discussed yet. I just finished reading Programming Principles and Practices using C++ and it was everything I expected, except it's explanation of virtual functions using it's API of FLTK.

I want a book that's going to teach me the C++ library and how C++ operates. As you can see I'm confused on how things are stored and how they differentiate between difference kinds of types. I understand stack vs heap but there's more to it. I would like to understand the inner-workings of C++ or least something towards that path.



Q1: The temporary value would normally be copied before it was destroyed so it could be returned, and possibly then copied again, which is two expensive operations. Instead, with move semantics you just steal the internal state a couple times in O(1) time.

Q2: C++ guarantees that when a const reference binds to a temporary, the lifetime of the temporary is extended. I don't know why it is only for const references and not normal references because you can call non-const functions on the returned-value (if you don't assign it to a const reference) but it is considered bad practice to do so (I also don't know why).

Q3: Tried "Effective STL" despite the bad name? http://stackoverflow.com/q/388242/1959975
> It introduces move semantics and instructs you to get introduce to the topic elsewhere.
> I have some questions.


These are two excellent articles on rvalue references and move semantics; you should be able to find answers to your questions in them.
http://thbecker.net/articles/rvalue_references/section_01.html
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/



> I'm on the third chapter and I'm so far disappointed.
> It uses examples to explain topics by using parts of the standard library that hasn't been discussed yet
> I want a book that's going to teach me the C++ library and how C++ operates.
> As you can see I'm confused on how things are stored
> and how they differentiate between difference kinds of types.

Have a little patience; you are still on the third chapter. The STL part of library is discussed, (and discussed very well), in chapters six to thirteen of the book; wait till you get there. The book that you already have is the best book that teaches STL.

I would very strongly urge you to stay with it. If you want, you can skim through chapters one to five, get to chapter six, and then, as and when the need arises, go back and have a deeper look at the relevant part that you had breezed through earlier.
Topic archived. No new replies allowed.