Comments (4)
Hi @jdgarciauc3m, could you please provide a code snippet which raises the warning you are seeing?
from gsl.
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.
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.
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)
- β Please take the GSL GitHub survey HOT 1
- Question: How to print a gsl::byte? HOT 2
- How to use the smartpointer overloads inside `<span_ext>`? HOT 1
- Purpose of `<span_ext>`
- gsl::not_null at compile time HOT 13
- Provide a "safe" and "simple" way for binary I/O HOT 2
- gsl.pc and gsl/gsl_rng.h files not found HOT 2
- CMake Error at CMakeLists.txt:14 (find_package): Could not find a package configuration file provided by "Microsoft.GSL" HOT 2
- Missing `swap` for `gsl::not_null` for move-only types HOT 4
- `GSL_SUPPRESS(y)` expands to `[[gsl::suppress("x")]]` for clang HOT 5
- Can not build with gcc13 because suggestion warning HOT 3
- gsl::not_null and std::variant are not fully compatible
- Reproducible build? HOT 2
- [Help] About the signed copy of my CLA HOT 1
- C++20 Modules Support? HOT 3
- how to do bound check for vector::front and vector::back HOT 2
- MSVC: Recent commits break compilation
- Turning off clang unsafe buffer warnings fails when compiling with a gcc based compiler HOT 3
- When will a new version be released? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gsl.