Skip to content
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

redundant(?) double static_cast #134

Closed
danra opened this issue Mar 13, 2019 · 1 comment
Closed

redundant(?) double static_cast #134

danra opened this issue Mar 13, 2019 · 1 comment

Comments

@danra
Copy link

danra commented Mar 13, 2019

The following:
int a = (int)1.0;

translates into
int a = static_cast<int>(static_cast<int>(1.0));

See https://cppinsights.io/lnk?code=aW50IG1haW4oKQp7CiAgCWludCBhID0gKGludCkxLjA7Cn0=&std=cpp17&rev=1.0

AFAICS there is a redundant static_cast<int> here - unless this is what the compiler actually does internally (for some reason?)

@andreasfertig
Copy link
Owner

Hello @danra,

I believe what C++ Insights shows is correct. Have a look here: https://godbolt.org/z/FC_pXU. It shows the AST output of your example and a second version with the C-Style-Cast. In C++ Insights the second version leads to only one static_cast. I assume the compiler does optimize this away in a later stage.

Andreas

@danra danra closed this as completed Mar 13, 2019
andreasfertig added a commit that referenced this issue Sep 28, 2019
…icit cast.

Both issues raised that there is a second `static_cast` after a C-style
cast. It turns out, that an `ImplicitCastExpr` has a method
`isPartOfExplicitCast`. This method together which the
`-show-all-implicit-casts` option is used to suppress the second cast
per default.
andreasfertig added a commit that referenced this issue Sep 28, 2019
Fixed #223 and #134: Suppress implicit casts which are part of a explicit cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants