Temp objects

Hi all,

A quick question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

double t = 5;

double test() {
	return t;
}

int main() 
{
	test() = 14;

	cout << t << '\n';

	cin.get();
	return 0;
}


It raises an error saying: the lvalue is not modifiable
I assume the returned value is a temporary object without an address to be able to refer to it. My question is, where is that temp object stored? In a register in the CPU, please? Does it have an address surprisingly?
Last edited on
Try
double & test()


Please include your include s!
Last edited on
I didn't ask how to fix the error (I know how); I wanted answers for my questions! :)
#include <iostream>
> test() = 14;
You're returning t by value, so all you're trying to say is
5 = 14;

5 is something you're not going to be able to change, hence the "lvalue is not modifiable".
Guys, thank you all so much but I know these!!! :(
Let me ask my questions once gain, please.

My question is, where is that temp object stored? In a register in the CPU, please? Does it have an address surprisingly?
@frek, I don't think it is a clean answer: an optimising compiler can do what it likes with a value that it knows can never be stored!

You can try googling
"where is the return value of a function stored"



Does it have an address surprisingly?

I think your translator is throwing a wobbly.
Last edited on
Depends on your system, but here's one set of answers: https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019

"A scalar return value that can fit into 64 bits is returned through RAX"

RAX is a register.
Topic archived. No new replies allowed.