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:
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.
using namespace std;
vector<int> doubleValues (const vector<int>& v)
for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr; ++itr )
new_values.push_back( 2 * *itr );
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.
"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?
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.