Coder Social home page Coder Social logo

Comments (3)

martinmoene avatar martinmoene commented on May 26, 2024

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 sets
    • gsl_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.

mbeutel avatar mbeutel commented on May 26, 2024

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 a narrow_cast operation for specifying that narrowing is acceptable and a narrow ("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.

mbeutel avatar mbeutel commented on May 26, 2024

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)

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.