If a function returns an int, can it be assigned by an int value? I don’t see it makes too much sense to assign a value to a function.
int f() {}
f() = 1;
I noticed that, if the function returns a reference to an int, it is ok. Is it restricted only to int? how about other types? or any other rules?
int& f() {}
f() = 1;
The first function returns an integer by-value, which is an r-value. You can’t assign to an r-value in general. The second f() returns a reference to an integer, which is a l-value – so you can assign to it.
Note: you don’t assign a value to the function, you just assign to its return value.
Be careful with the following:
You’re returning a reference to a temporary, which is no longer valid after the function returns. Accessing the reference invokes undefined behaviour.