-
-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imprecise translation for ternary operator #324
Comments
Hello @lSoleyl, that is a nice one. Thanks for spotting and reporting this issue. I agree that the expression needs some additional parenthesis. A fix for that is on its way. To you second part, and thanks for reporting the C++17 version first, this is correct. However, I agree that it looks wired. I assume this was either a bug in Clang or the language rules changed with C++17. The additional Andreas The following is for reference for others and myself. This entire code surprised me, because my initial thought was that even the struct Str {
explicit Str(const char* string) : string(string) {}
operator const char*() const {
return string;
}
const char* string;
};
Str globalString{"test"};
const char* getString(bool empty) {
return empty ? "" : globalString;
} You can test it here: cppinsights.io/s/2f2b90f3 |
Thanks for the quick response and the link to the compiler explorer... I didn't knew about this one.
This example was originally crafted to be a C++ quiz to explain why The actual explanation for the construction of return empty ? [const char[1]]"" : [Str&]globalString;
//--> Both types are not equal and there is no conversion from Str& to const char[1] (const char* is not considered equal here)
// So following conversion path is chosen:
return empty ? [Str] Str("") : [Str&] globalString;
// --> Now there is still a mismatch because we have a temporary and an L-Value, which is fixed by copying the second expression
return empty ? [Str] Str("") : [Str] Str(globalString);
// --> And now the implicit conversion to const char* can be called on the result to return the function's expected type
return [const char*] (emtpy ? Str("") : Str(globalString)).operator const char*(); So now one can also see that if In your second example with the
I just want to add that I don't think, this really is a performance issue in the |
Hello @lSoleyl, my pleasure.
thanks for clarifying the origin of the code. I figured something like this.
Agreed. For that piece of code it doesn't matter. And yes, AST is way to early to see the final result. More or less no optimization takes place at this stage. My note was about a more expensive to setup object when the compile cannot optimize it away so easily. Andreas |
Hi,
this is a great tool for crosschecking the compiler's interpretation of C++ code. However the expansion of the ternary operator looks a bit misleading to me. Given the following code:
With C++17 the
getString()
function gets translated into:I would argue that a parenthesis is missing and it should look like this for both result types of the ternary expression to match:
Also when setting the compilation option to anything below C++17, then the result looks even weirder:
The text was updated successfully, but these errors were encountered: