Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualCornstalks

Posted 04 April 2013 - 05:03 PM

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?

It's kind of a weird part of C++. The reference/object exists, and it will exist for the duration of the function, but it might not really exist... Confusing? A little. There are two types of existance. The "readonly" type of existence, and the "read+write" type of existence. The object exists (in terms of being able to read it) as far as 12.2.5 is concerned. "existence" doesn't mean you can write to an object. It just means it's there.

 

I suggest reading this. One of the big reasons for this whole thing is to avoid bugs where temporaries are accidentally modified. (think of it, being able to do 2 = 1; would be weird...)

 

I also suggest reading this. It might not seem like it makes a lot of sense, and in some ways, it doesn't, but it's just one of those things in C++.


#4Cornstalks

Posted 04 April 2013 - 05:01 PM

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?

It's kind of a weird part of C++. The reference/object exists, and it will exist for the duration of the function, but it might not really exist... Confusing? A little. There are two types of existance. The "readonly" type of existence, and the "read+write" type of existence. The object exists (in terms of being able to read it) as far as 12.2.5 is concerned. "existence" doesn't mean you can write to an object. It just means it's there.

 

I suggest reading this. One of the big reasons for this whole thing is to avoid bugs where temporaries are accidentally modified.

 

I also suggest reading this. It might not seem like it makes a lot of sense, and in some ways, it doesn't, but it's just one of those things in C++.


#3Cornstalks

Posted 04 April 2013 - 04:57 PM

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?

It's kind of a weird part of C++. The reference/object exists, and it will exist for the duration of the function, but it might not really exist... Confusing? A little. There are two types of existance. The "readonly" type of existence, and the "read+write" type of existence. The object exists (in terms of being able to read it) as far as 12.2.5 is concerned. "existence" doesn't mean you can write to an object. It just means it's there.

 

I suggest reading this. One of the big reasons for this whole thing is to avoid bugs where temporaries are accidentally modified.

 

To expand on a paragraph in that link, the compiler is allowed to re-use temporary objects in an expression (so ((str + " ") + "b") might only require one temporary instead of several). If you could modify a temporary, you'd screw up this optimization.


#2Cornstalks

Posted 04 April 2013 - 04:56 PM

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?

It's kind of a weird part of C++. The reference/object exists, and it will exist for the duration of the function, but it might not really exist... Confusing? A little. There are two types of existance. The "readonly" type of existence, and the "read+write" type of existence. The object exists (in terms of being able to read it) as far as 12.2.5 is concerned. "existence" doesn't mean you can write to an object.

 

I suggest reading this. One of the big reasons for this whole thing is to avoid bugs where temporaries are accidentally modified.

 

To expand on a paragraph in that link, the compiler is allowed to re-use temporary objects in an expression (so ((str + " ") + "b") might only require one temporary instead of several). If you could modify a temporary, you'd screw up this optimization.


#1Cornstalks

Posted 04 April 2013 - 04:56 PM

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?

It's kind of a weird part of C++. The reference exists, and it will exist for the duration of the function, but it might not really exist... Confusing? A little. There are two types of existance. The "readonly" type of existence, and the "read+write" type of existence. The object exists (in terms of being able to read it) as far as 12.2.5 is concerned. "existence" doesn't mean you can write to an object.

 

I suggest reading this. One of the big reasons for this whole thing is to avoid bugs where temporaries are accidentally modified.

 

To expand on a paragraph in that link, the compiler is allowed to re-use temporary objects in an expression (so ((str + " ") + "b") might only require one temporary instead of several). If you could modify a temporary, you'd screw up this optimization.


PARTNERS