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

Interaction between unsigned and pointer/array #394

Open
danakj opened this issue Oct 2, 2023 · 1 comment
Open

Interaction between unsigned and pointer/array #394

danakj opened this issue Oct 2, 2023 · 1 comment
Labels
design Design of the library systems as a whole, such as concepts
Milestone

Comments

@danakj
Copy link
Collaborator

danakj commented Oct 2, 2023

Using an operator[] on a type that takes size_t works great with unsigned types. They convert to primitives.

However operator[] on a pointer is built in and takes a signed ptrdiff_t. Converting from an unsigned value to signed ptrdiff_t could overflow and thus needs to check(). But we want implicit signed conversions to not compile.

There's no way I can think of to limit the conversions to only happening inside ptr[x]. If the compiler would convert that to *(ptr + x) then it would work fine, but it doesn't. It needs x to become ptrdiff_t.

Allowing conversion to ptrdiff_t also allows conversion to long (on linux) and int64_t which is much too broad.

What do?

  • A method on unsigned types to check+convert?
  • Expect code with pointer indexing to be converted to span/array/vector/etc first? This should be happening regardless but ordering dependencies are not good.
  • Rewrite it to *(ptr + x)?
  • Introduce -funsigned-pointer-index which adds a size_t overload???
  • ???
@danakj danakj added the design Design of the library systems as a whole, such as concepts label Oct 2, 2023
@danakj danakj added this to the stable-numerics milestone Oct 2, 2023
@danakj
Copy link
Collaborator Author

danakj commented Nov 29, 2023

TODO: Make the better problem description doc public, and bring some kind of proposal to Clang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of the library systems as a whole, such as concepts
Projects
None yet
Development

No branches or pull requests

1 participant