Skip to content

gsl::narrow() terminates rather than throws #872

@londey

Description

@londey

gsl::narrow's implementation has been changed to terminate rather than throw as documented in GSL and the C++ Core Guidelines.

The change appears to have been made while changing unit test frameworks here.
2b10729

GSL/include/gsl/gsl_util

Lines 108 to 122 in 9f6a9a5

// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
template <class T, class U>
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
#if GSL_CONSTEXPR_NARROW
constexpr
#endif
T narrow(U u) noexcept(false)
{
T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) gsl::details::terminate();
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
gsl::details::terminate();
return t;
}

Was this change intentional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions