4

The situation of my interest is

const int &n1 = 123;
const int &n2 = 123;

I know it is something as if the literal 123 is the parameter for initializing a temporary int and const is just a boring compile time check, but I want to know the reason why distinct temporaries are needed in this case, instead of n1 and n2 both having the same temporary.

I know the rule exists but do not know why this rule exists.

Kindred
  • 1,229
  • 14
  • 41

2 Answers2

4
const int &n1 = 123;
const int &n2 = 123;

I want to know the reason why distinct temporaries are needed in this case.

Because the C++ committee probably didn't care for this specific case. What was on their mind, what was their aim, was to provide rules on how temporary are handled in the more useful and common case: evaluation of full-expression chaining creation, use and destruction of temporaries:

class A { /* ... */ };
A make_a();
void consume_a(A&&);
void use_a(A const&);

consume_a(make_a());
use_a(make_a());

It's obvious make_a() needs to produce a different temporary A each time.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • @ptr_user7813604 see [this answer](https://stackoverflow.com/a/53422288/5470596) on the question you linked. – YSC Nov 22 '18 at 10:15
  • Your answer is valid of course but in my opinion the C++ committee _also_ cares for this simple case, for compatibility reason – Damien Nov 22 '18 at 10:58
  • @Damien I didn't say otherwise. – YSC Nov 22 '18 at 14:21
0

If you want them to have the same address, you can always do const int &n2 = n1. If you do it in another way, the compiler may suspect you have your own reason to do that.

The compiler is not allowed to guess what your concern is. It implements what you write. Doing an optimisation as your suggest would imply that the comparison bool test = &n1 == &n2 will give another result. Generally speaking, the compiler is allowed to make optimisation, as long as the result is not modified.

You should consider that previous compilers were much less efficient than now. Such an optimisation or a language feature should have been impossible 30 years ago. So, it if it is not an optimisation, it will be a modification of the language that would potentially change the behaviour of many existing programs.

Damien
  • 4,809
  • 4
  • 15
  • 20
  • So your `&n1 == &n2` is based on rvalue-reference, or the one I used in my question? – Kindred Nov 22 '18 at 09:37
  • @ptr_user7813604 I don't make reference specifically to rvalue, I just checked the addresses of variables n1 and n2. I should have written `bool test = &n1 == &n2`. The fact that such a test looks strange is not the point. It is legal and will give another result is the compiler performs an optimisation as you suggest – Damien Nov 22 '18 at 09:41
  • I hope it answers your question. In a first step, reading your question, I did not understand immediately why this optimisation was not performed. – Damien Nov 22 '18 at 09:44
  • Yes, the result will change – Damien Nov 22 '18 at 09:47
  • Then isn't that you *assume* my idea is an optimization version in the first place? (btw, this comment was appeared above your last one, lol) – Kindred Nov 22 '18 at 09:48
  • I mean: you think my version cannot be done because it violate the rule that optimization should not change the outcome, but I'm thinking about __*if*__ the compiler implement the version in my question in the first place, then will your answer still solve my problem. (I appreciate your effort and I'm try not be annoying but my upvote/accepted will be meaningless if I don't really solve my question.) – Kindred Nov 22 '18 at 09:52
  • I understood it like that. Your case look simple. But let us assume that you are dealing with const objects much more complicated, with these instructions at different places, possibly in different functions ... – Damien Nov 22 '18 at 09:53
  • 1
    You should consider that previous compilers were much more less efficient than now. Such an optimisation or a language feature should have been impossible 30 years ago. So, it if it is not an optimisation, it will be a modification of the language that would potentially change the behaviour of many existing programs – Damien Nov 22 '18 at 09:56
  • OK, I think my conclusion would probably be a combination of what @n.m. said in the comment above and your like this: So under no explicit optimization options, compiler won't distinguish prvalue/object as the rvalue assigned to the reference, it just _assume_ that it's the worst case, object, so it create distinct spaces, and further optimization is whatsoever done after that. Could this reasonable? – Kindred Nov 22 '18 at 10:05
  • To be honest, I am not very used to rvalue concept. I am much more familiar with old KR C programs written a long time ago, and I see that you proposal will cause compatibility issues. I will edit my answer – Damien Nov 22 '18 at 10:16
  • No my intention is not to propose anything, instead, I'm asking the intention of the proposal so I pointed out another option, but yes my description will probably make you think like that. – Kindred Nov 22 '18 at 10:19
  • Then just don't care about rvalue, I mean the value that can just be read. I hate terminology sometimes since difference definition-phrasing may blur the truth. – Kindred Nov 22 '18 at 10:22
  • I think we agree. Note: this discussion is long, we will have to delete some comments – Damien Nov 22 '18 at 10:36
  • Thanks for your help, but I won't prefer delete our discussion because we've spent time on it, and people can have more clear understanding about which part we might have missed. But I cannot stop you. I'll give you upvote for your effort. – Kindred Nov 22 '18 at 10:59