Search code examples
c++functionlvalue

what does func() = var_name; assignment do in c++?


I was studying about l-values and r-values but I am confused with this:

#include<iostream>
int& get_val(){
    return 10;
}
int main(){
    get_val() = 5;
    int a = get_val();
    std::cout<<a<<std::endl;

    return 0;
}

I know about int a = get_val(); (i.e., it will assign the returned value to the variable), but I want to know what this does: get_val() = 5;. What does int& get_val() do?

I know we cannot run this code but I want to learn the concept behind this.


Solution

  • Your get_val function is defined as returning a reference to an integer; however, what you actually return is a reference to a constant, so any attempt to assign a value to that will not compile. In fact, the function itself (attempting to return a reference to 10) won't compile; clang-cl gives this:

    error : non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'

    However, if you add a static int1 inside your function, and return (a reference to) that, then you can use statements like getval() = x; to assign the value of x to that internal static int (which is otherwise 'invisible' outside the function).

    Here's a short demonstration:

    #include <iostream>
    
    int& get_val()
    {
        static int inside = 42;
        std::cout << inside << "\n";
        return inside;
    }
    
    int main()
    {
        get_val() = 5; // Prints INITIAL value of "inside" then (after the call) assigns
                       // the value 5 to it, via the returned reference
        get_val();     // Prints the modified value
        return 0;
    }
    

    The above example is trivial and would likely not serve any real-world purpose; it is purely for demonstration purposes. However, there may be cases where you would want a function to return a reference to any one of a number of different objects, depending on calculations and decisions made in that function; the selected, referenced object can then be suitably modified using code like that shown here.


    1 The inside variable needs to be declared as static so that:

    1. Its lifetime does not end when the function returns (thus creating a dangling reference).
    2. Its value is maintained from one call to the next.