Comments (3)
For example:
Maintain compatibility with M-GSL and restore default throwing behavior of narrow() and provide:
gsl_CONFIG_NARROW_DISCARD_THROWS
/gsl_CONFIG_NARROW_DISCARD_TERMINATES
gsl_NO_EXCEPTIONS
which setsgsl_CONFIG_CONTRACT_VIOLATION_TERMINATES
gsl_CONFIG_NARROW_DISCARD_TERMINATES
or only provide the following to be able to have old behavior compatible with M-GSL:
gsl_CONFIG_NARROW_DISCARD_THROWS
or ...
from gsl-lite.
I agree that your implementation of narrow()
is probably more useful, but it clearly isn't what the Core Guidelines specify:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#note-204
double d = 7.9;...
The guidelines support library offers anarrow_cast
operation for specifying that narrowing is acceptable and anarrow
("narrow if") that throws an exception if a narrowing would throw away information:i = narrow_cast<int>(d); // OK (you asked for it): narrowing: i becomes 7 i = narrow<int>(d); // OK: throws narrowing_error
So there is no way to get narrowing with a fail-fast check. I think I'd have specified narrow<>()
such that it uses Expects()
to guard against loss of information, and an additional narror_or_throw<>()
which throws narrowing_error
.
Cf. isocpp/CppCoreGuidelines#1498 and microsoft/GSL#802.
from gsl-lite.
From isocpp/CppCoreGuidelines#1498:
Editors' call: Our view is that narrow is like to_integer(string) -- if the input contains an invalid value (e.g., to_integer("hello")) then we should throw a run-time error. This means that narrow should not use an Expects, it is not a precondition violation.
So perhaps our narrow<>()
shouldn't do that either.
I'm inclined to add a configuration option gsl_CONFIG_NARROW_IS_PRECONDITION_CHECK
, have it default to 0 (current behavior), and to change the default to 1 (conforming behavior) for the next major version.
I do share the desire for a narrowing cast that uses a precondition check. Perhaps we can add one as an extension, but I guess it should have a different name.
from gsl-lite.
Related Issues (20)
- `not_null<>` should be hashable HOT 2
- Should `not_null<>` be two different classes? HOT 3
- "illegal instruction" Error when using gsl::span as Kernel Parameter HOT 3
- Test compilation failures on GCC 11: error: array subscript -1 is outside array bounds of 'char[6]' [-Werror=array-bounds] HOT 7
- Test failures on s390x (GCC 10.2.1 and 11.0.1) HOT 7
- Support `narrow<>()` for types without total order
- `not_null<shared_ptr<T>>` not convertible to `weak_ptr<T>` HOT 1
- gsl_FEATURE_GSL_LITE_NAMESPACE not working HOT 3
- Definition of `fail_fast` and of `narrowing_error` triggers `clang`'s `-Wweak-vtables` HOT 2
- gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME in kernel code HOT 1
- including gsl-lite triggers `-Wuseless-cast` warnings HOT 2
- Add tests for `not_null_ic<>`
- Tests fail to compile due to -Werror=type-limits when char is unsigned HOT 6
- Add CI for non-x86 architectures
- Allow `not_null<void*>` and related. HOT 9
- `not_null`'s evaluation of nullness can lead to *very* cryptic errors. HOT 10
- Suppress warnings in lest via system include
- Comparison of `std::optional<not_null<T>>` and `not_null<T>` is ambiguous HOT 2
- gsl::span works differently than std::span in terms of type matching? HOT 2
- 0.41.0: not ready to be build with `-D_FORTIFY_SOURCE=3` HOT 17
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-lite.