Giter VIP home page Giter VIP logo

Comments (4)

dmitrykobets-msft avatar dmitrykobets-msft commented on June 12, 2024

Hi @jdgarciauc3m, could you please provide a code snippet which raises the warning you are seeing?

from gsl.

jdgarciauc3m avatar jdgarciauc3m commented on June 12, 2024

Sure!

See the following piece of code

  std::array v {1, 2, 3};
  gsl::span t{v};
  gsl::index i = 1;
  auto x = t[i];
  std::cout << x << '\n';

When compiled with clang++-15 and the following flags: -Wall -Wextra -Weffc++ -Werror -pedantic -pedantic-errors -Wconversion, the following error is obtained:

[build] error: implicit conversion changes signedness: 'gsl::index' (aka 'long') to 'gsl::span::size_type' (aka 'unsigned long') [-Werror,-Wsign-conversion]
[build]   auto x = t[i];
[build]            ~ ^
[build] 1 error generated.

Similar error is obtained with gcc-12 and -Wsign-conversion.

from gsl.

dmitrykobets-msft avatar dmitrykobets-msft commented on June 12, 2024

Thanks for the repro.
The warning that appears is not about the span's iterator's index operator, but rather about the mismatch in types between gsl::index (which is signed), and the span's operator[] parameter (which is unsigned), in the expression auto x = t[i];

The choice of types appears to be by design. ES.107: Don’t use unsigned for subscripts, prefer gsl::index encourages the use of a signed index type for safety reasons, but points out that the C++ standard is still using unsigned indices. This other discussion reaffirms the design decision behind wanting signed indices. At one point, gsl::span's operator[] did indeed accept a signed parameter, but this was changed in order to conform with std::span. If I replace gsl::span with std::span in your code example I get the same warning.
Unfortunately, while the standard containers continue to accept unsigned indices, the use of gsl::index will trigger conversion warnings if they are enabled globally by default, hence the pessimistic enforcement section under ES.107.

As for your question regarding the mismatch between span's and its iterator's operator[], I'll need to look a bit deeper first before getting back to you.

from gsl.

dmitrykobets-msft avatar dmitrykobets-msft commented on June 12, 2024

Update, regarding the difference between span's and its iterator's operator[] parameter:

This is again, by design. A span does not have elements prior to the 0 index, whereas its iterator can point anywhere in its range, meaning negative indexing is perfectly valid. The following code will raise the same warning you were seeing before, except now complaining about converting from an unsigned value to a signed value:

  std::array v {1, 2, 3};
  gsl::span t{v};
  size_t i = 1;
  auto x = t.begin()[i]; // implicit conversion changes signedness: 'size_t' (aka 'unsigned long') to 'gsl::details::span_iterator::difference_type' (aka 'long') [-Werror,-Wsign-conversion]
  std::cout << x << '\n';

The same is true for std::span.

from gsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.