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

static_cast instead of reinterpret_cast for interpreting pointer to integral conversion #512

Closed
napierson opened this issue Jan 17, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@napierson
Copy link

See for example https://cppinsights.io/lnk?code=bG9uZyBsb25nIGludCBjaGFyUHRyVG9JbnQoY29uc3QgY2hhciogc3RyKQp7CiAgICBsb25nIGxvbmcgaW50IGEgPSAobG9uZyBsb25nIGludClzdHI7CiAgICByZXR1cm4gYTsKfQoKaW50IG1haW4oKSB7CiAgICBjb25zdCBjaGFyKiBzdHIgPSAiMTIzIjsKICAgIGNoYXJQdHJUb0ludChzdHIpOwp9&insightsOptions=cpp17&std=cpp17&rev=1.0

Source:

long long int charPtrToInt(const char* str)
{
    long long int a = (long long int)str;
    return a;
}

int main() {
    const char* str = "123";
    charPtrToInt(str);
}

Output:

long long charPtrToInt(const char * str)
{
  long long a = static_cast<long long>(str);
  return a;
}


int main()
{
  const char * str = "123";
  charPtrToInt(str);
  return 0;
}

But the conversion from const char* to long long is being accomplished by a reinterpret_cast<long long>, not a static_cast<long long>. If you try to substitute in the static_cast, the code fails to compile.

@andreasfertig
Copy link
Owner

Hello @napierson,

thanks for reporting this issue! That was a missing cast. C++ Insights only supported the other way around, IntegralToPointer.

A fix is on its way.

Andreas

@andreasfertig andreasfertig added the bug Something isn't working label Jan 20, 2023
andreasfertig added a commit that referenced this issue Jan 20, 2023
Fixed #512: added missing PointerToIntegral conversion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants