Unexpected program flow (templates / conversion operator).

Started by
14 comments, last by Cornstalks 11 years, 3 months ago
"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.
Advertisement
You could drop the = T* operator and allow assignment only from another Wrap<T>.

But yes, this type of thing can make code behave in unexpected ways. Even std::auto_ptr is deprecated for that reason.
Yeah, dropped the =(T*) operator using a separate function instead in my testbed - probably the way to go (have not yet plugged into main app). Will investigate further tomorrow (evaluate how much of a hassle it will be in real case).

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.

It's common practice to word it like that in English, yes, both in the professional workplace and in academic universities. English is, in my opinion, a pretty bad language overall and there are tons of times where what we say isn't what we really mean :) I agree that "reference to (non) const" is more technically correct (and is typically used in formal, technical settings), but "(non) const reference" is so common it's not going to go away any time soon. People tend to use it more because it's shorter (and we're lazy in English, unfortunately).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
As the forum edit function most likely screws up the whole post - i reply to myself instead.
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" ;).
Ugh, as one of our teachers used to say: "the mouth is singing and the hearth worrying" - that really should be "reference to non-const".
Given that the C++ Standard uses the terms "const reference" and "non-const reference" it seems that they are, in fact, correct terms. E.g. section 5.2.2 paragraph 5 of ISO/IEC 14882:1998 has the line "Where a parameter is of const reference type a temporary object is introduced if needed...."
Given that the C++ Standard uses the terms "const reference" and "non-const reference" it seems that they are, in fact, correct terms. E.g. section 5.2.2 paragraph 5 of ISO/IEC 14882:1998 has the line "Where a parameter is of const reference type a temporary object is introduced if needed...."

Sweet, I was wondering if it did (and was too lazy to dig through it to see)

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement