Rvalue and Lvalue Links

I have a printInt function as shown below.

void printInt(const int& a)
{
    cout<<a<<endl;
}

When I call a function using the following arguments, for example

int a=5;
printInt(a);
printInt(5);

It works great. But when I change the definition of a function to

void printInt(int& a)
{
    cout<<a<<endl;
}

This gives an error to call printInt(5). Now my question is why const int&is both an lvalue reference and an rvalue, whereas it int&is only an lvalue reference. As far as I know, int&&is an rvalue reference. So, how can one refer to an rvalue link?

To summarize my problem:

  • Lvalue control parameter

    void printInt(int& a)
    {
        cout<<a<<endl;
    }
    
  • Rvalue control parameter

    void printInt(int&& a)
    {
        cout<<a<<endl;
    }    
    
  • Both lvalue and rvalue. but how?

    void printInt(const int& a)
    {
        cout<<a<<endl;
    }
    
+4
source share
1 answer

const int& lvalue. , , rvalues. int& lvalue, . , - .

+8

All Articles