CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Should gsl::narrow include slicing

Open JordanMaples opened this issue 4 years ago • 1 comments

Re: Microsoft/GSL#917 Should narrow and narrow_cast also apply to slicing, because slicing also allows for going from larger to smaller ranges of value. Both could be considered lossy.

Thanks.

JordanMaples avatar Aug 26 '20 22:08 JordanMaples

Also see: https://github.com/microsoft/GSL/pull/900

The C++ Core Guidelines simply specify gsl::narrow as narrow // narrow<T>(x) is static_cast<T>(x) if static_cast<T>(x) == x or it throws narrowing_error. The implementation uses relational operators, so I can't do gsl::narrow<point<unsigned>>(point<int>{}) for my point type.

JohelEGP avatar Aug 26 '20 22:08 JohelEGP