Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualtanzanite7

Posted 15 January 2013 - 04:45 PM

"foo(7*6)" with reference to non-const "foo" function gives in VC: "initial value of reference to non-const must be an lvalue" - perfectly reasonable. Did not realize compiler is willing to create a new temporary constant object in this case - thanks, me likes.

However. My actual Emit::get() code is like:
Wrap<T> get() {
    Wrap<T> result;
    ... more stuff
    return result;
}
The internal copy of "result" may not be destroyed before it is returned - i fail to see the relevance of the given example (call me dense). I do not see any reason why the compiler would care what happens with "result" before it is destroyed - Wrap<T> destructor tells all it needs to know. Ie. it is not a compiler temporary created for referencing purposes - it is local variable which also happens to be returned and hopefully short-circuited by return value optimization.

Also, using "explicit" and forcing it to use the correct copy constructor does get around that problem (plugged into my actual code and it passed all my test cases - as far as i can see it does exactly what i expect it to do). Acceptable workaround (would prefer the constructor not being "explicit" tho).

Assignment, however is much more problematic (no "explicit" keyword to hide the "wrong" one) - if i comment the other one out, and hence force it to use the right one, then it works. Cannot make the type conversion explicit either sad.png (that would fix all the problem without needing any workarounds, unfortunately - not an acceptable loss).

Irritating. Prefers a function it can only use through implicit type conversion to the function that matches from start :/.

3) Unusual, perhaps, but perfectly reasonable. It is not copy-assignment nor move-assignment (it only looks like move assignment because it is a minimized example-case) - it is just an assignment that does stuff that is expected to happen when one uses assignment with the relevant object types.


1) If you try to force the language to do the unexpected, like having something that looks like like it might be an assignment operator not have the signature of an assignment operator and not work like an assignment operator, you should really expect your program to do the unexpected.

2) Also, there is a significant difference between a const reference and a reference to a const. One is "int const&" the other is "int& const"... can you guess which is which, and which one "const int&" is?


1) Unexpected != unusual. One would fully expect that particular class to behave exactly the way it does. What the hell are you blabbering about?
2) You did not pay any attention to what i have said, right? I know perfectly well what is what - i had problems digesting a response where parts of it made no sense.

why do you refer to "reference to non-const" as "non-const reference"?

Because that's how the rest of the world typically does it (at least in English). I guess you could technically call it a misnomer, maybe, but references themselves are always const (so it makes no sense to have a truly "non-const reference" (that is, a reference that can be changed to refer to another object; it's impossible in C++)), so when people talk about (non) const references, the (non) const always refers to the object that the reference refers to, not to the reference itself (since the reference itself is always const).


Thanks for the explanation. Is it something that is accepted in English CS classes? Correct terminology was quite important for us - unfortunately, none of it was in English, so i tend to be cautiously confused when i encounter something in English that does not seem to make any sense.

I recommend you and everyone else drop the incorrect usage - impossibility of "non-const reference" is an unacceptable reason to refrain using the perfectly valid "reference to const". VC agrees with me and also uses "reference to const" ;).

Sorry if i am too picky here.

edit: un-fuckup the forum formatting.

#1tanzanite7

Posted 15 January 2013 - 04:45 PM

"foo(7*6)" with reference to non-const "foo" function gives in VC: "initial value of reference to non-const must be an lvalue" - perfectly reasonable. Did not realize compiler is willing to create a new temporary constant object in this case - thanks, me likes.<br /><br />However. My actual Emit::get() code is like:<pre class="_prettyXprint _lang-code _linenums:NaN">
Wrap&lt;T&gt; get() {
Wrap&lt;T&gt; result;
... more stuff
return result;
}
</pre>The internal copy of "result" may not be destroyed before it is returned - i fail to see the relevance of the given example (call me dense). I do not see any reason why the compiler would care what happens with "result" before it is destroyed - Wrap&lt;T&gt; destructor tells all it needs to know. Ie. it is not a compiler temporary created for referencing purposes - it is local variable which also happens to be returned and hopefully short-circuited by return value optimization.<br /><br />Also, using "explicit" and forcing it to use the correct copy constructor does get around that problem (plugged into my actual code and it passed all my test cases - as far as i can see it does exactly what i expect it to do). Acceptable workaround (would prefer the constructor not being "explicit" tho).<br /><br />Assignment, however is much more problematic (no "explicit" keyword to hide the "wrong" one) - if i comment the other one out, and hence force it to use the right one, then it works. Cannot make the type conversion explicit either <img data-cke-saved-src="http://public.gamedev.net//public/style_emoticons/default/sad.png" src="http://public.gamedev.net//public/style_emoticons/default/sad.png" class="bbc_emoticon" title=":(" /> (that would fix all the problem without needing any workarounds, unfortunately - not an acceptable loss).<br /><br />Irritating. Prefers a function it can only use through implicit type conversion to the function that matches from start :/.<br /><blockquote class="ipsBlockquote" data-author="Bregma" data-cid="5021934"><p><br /></p><blockquote class="ipsBlockquote" data-author="'tanzanite7'"><p>3) Unusual, perhaps, but perfectly reasonable. It is not copy-assignment nor move-assignment (it only looks like move assignment because it is a minimized example-case) - it is just an assignment that does stuff that is expected to happen when one uses assignment with the relevant object types.</p></blockquote><br />1) If you try to force the language to do the unexpected, like having something that looks like like it might be an assignment operator not have the signature of an assignment operator and not work like an assignment operator, you should really expect your program to do the unexpected.<br /><br />2) Also, there is a significant difference between a const reference and a reference to a const. One is "<span style="font-family:courier new;">int const&amp;</span>" the other is "<span style="font-family:courier new;">int&amp; const</span>"... can you guess which is which, and which one "<span style="font-family:courier new;">const int&amp;</span>" is?<br /><p><br /></p></blockquote>1) Unexpected != unusual. One would fully expect that particular class to behave exactly the way it does. What the hell are you blabbering about?<br />2) You did not pay any attention to what i have said, right? I know perfectly well what is what - i had problems digesting a response where parts of it made no sense.<br /><blockquote class="ipsBlockquote" data-author="Cornstalks" data-cid="5021941"><p><br /></p><blockquote class="ipsBlockquote" data-author="tanzanite7" data-cid="5021932"><p>why do you refer to "reference to non-const" as "non-const reference"?</p></blockquote>Because that's how the rest of the world typically does it (at least in English). I guess you could technically call it a misnomer, maybe, but references themselves are always const (so it makes no sense to have a truly "non-const reference" (that is, a reference that can be changed to refer to another object; it's impossible in C++)), so when people talk about (non) const references, the (non) const always refers to the object that the reference refers to, not to the reference itself (since the reference itself is always const).<br /><p><br /></p></blockquote>Thanks for the explanation. Is it something that is accepted in English CS classes? Correct terminology was quite important for us - unfortunately, none of it was in English, so i tend to be cautiously confused when i encounter something in English that does not seem to make any sense.<br /><br />I recommend you and everyone else drop the incorrect usage - impossibility of "non-const reference" is an unacceptable reason to refrain using the perfectly valid "reference to const". VC agrees with me and also uses "reference to const" ;).<br /><br />Sorry if i am too picky here.

PARTNERS