Index

SchoolNotes

  1. CS225
    1. OOP
      1. Inheritance
      2. R-Value References
    2. Misc Notes
    3. Size/Offsets of Structs/Classes
    4. Week 4
  2. CS200
    1. Introduction
    2. Scan Conversion
    3. Quiz 01
  3. GAM200
    1. Notes
  4. CS180
    1. Introduction
    2. Midterm Revision
    3. Finals Revision
    4. Memory
    5. ELF Format
    6. History of Computers
    7. Stuff to Research
    8. Quiz To-Do
    9. OS Architectures
    10. Week 3
    11. Week 4
    12. Week 5
    13. Week 6 (Threads)
    14. Week 7 (Scheduling)
    15. Week 8 (Thread Scheduling)
    16. Week 9 (Memory Management)

R-Value References

l-values


l-values are values that have a name and that I can do an address of operator on.

r-values


The proper term is there are
- prvalues
- xvalues (between the lvalue and the rvalue)
rvalues is the union of prvalues and xvalues
prvalues
- pure r values
- basically values without an identifier
- cannot take address of
e.g. 3+x, &(3+x) (can't be done)
- usually not allowed to assign to
e.g. 3+x =10;
xvalues
- expiring values (values that are about to die)
- usually refers to values returned by functions

rvalues can also be referred to as values that are not stored or retained if they are not assigned - however this can only be done for user defined types, because there is a default synthesized copy assignment operator for classes.
rvalues CAN be assigned to, even though it is messed up - In C++, it just checks whether the .operator=() can be used.

rvalue references



rvalue references are NOT rvalues! All references are l-values!


3. Why rvalue ref? Move semantics.
Move semantics is taking over the
resources of the rvalue ref object and
leaving the rvalue ref obj in a destructible state
which if destroyed, does not affect correctness.




int && a = 5//A is an rvalue ref.

template <typename T>
class hoho { T&& x };

hoho<Obj> h; //not deduced

template <typename T>
void laugh(T&& b);

laugh (x); //deduced

//Laugh takes in a universal reference. Depending on context, it can either be an lvalue ref or rvalue ref. (ie can be lvalue or rvalue depending on the thing passed in)

template <typename T>
class hoho {
template<typename U>
hoho(U&& u)
{

}
T&& x;
};

//U&& u is a universal reference in this case. 
//T&& x is an r-value reference because hoho is not deduced at run-time.




Str operator+(const Str& lhs, const Str& rhs)
{
   Str r(lhs);
   return r+=rhs;
}

/*Copy constructor is only called ONCE because of Copy Elision.


In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects. The C++ language standard generally allows implementations to perform any optimization, provided the resulting program's observable behavior is the same as if, i.e. pretending, the program was executed exactly as mandated by the standard.

Basically it removes copy constructing from a temporary lvalue into an rvalue.

When you do move construction, you assign the dying value to nullptr so that when it destructs, it won't have a dangling pointer. (This is equivalent to having no handphone and snatching a handphone and making sure he has no handphone after)
NOT for move assignment, however! (This is equivalent to both parties having a handphone, you swap the handphones and let him die)

We are only supposed to set up the object for destruction.

Why do perfect forwarding?

First, what is perfect forwarding?
Perfect forwarding is basically forwarding lvalue to lvalue, rvalue to rvalue.

template<typename T, typename U>
U* factory (T x) /*forces us to do a copy*/
{
    return new U(x);
}

template<typename T, typename U>
U* factory (T& x) /* rvalues cannot be bound to T&*/
{
    return new U(x);
}

template<typename T, typename U>
U* factory (const T& x)
{
    return new U(x); /* U ctor must be COPY ctor 
                        lvalue->lvalue rvalue->lvalue*/

}