dimanche 29 mai 2016

C++: rvalue reference memory

Since c++ provides references to rvalues i.e. rvalue references which are mainly used to perform move semantics and other memory efficient tasks. But in the following case reference is changing the value of an literal but as we know that literals are read only so how could a reference change the value of some read only variable. Does a rvalue reference allocate it's own memory or it simply changes the value of literal?

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int&& b = 3;
    int& c = a;
    b++;
    c++;
    cout << " Value for b " << b << " Value for c " << c << endl;
}

Secondly, when a temporary object is assigned with a reference, reference works with the data of that object. But according to the definition of temporary objects they are deleted as when the expression using them ends. How could the reference act as an alias name to that temporary object if that temporary object is out of memory?

Aucun commentaire:

Enregistrer un commentaire