mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
32511b8de4
Some checks are pending
CI_Android / Android (push) Waiting to run
cmake_find_package / Build ${{ matrix.os }} (macos-latest) (push) Waiting to run
cmake_find_package / Build ${{ matrix.os }} (ubuntu-latest) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 14, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 14, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 17, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 17, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 20, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 20, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 20, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 23, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Debug, 23, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 14, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 14, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 14, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 17, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 17, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 17, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 20, 10) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 20, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 20, 12) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 23, 11) (push) Waiting to run
Compiler Integration Tests / gcc (Release, 23, 12) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 13, 14) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 13, 17) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 13, 20) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 13, 23) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 14, 14) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 14, 17) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 14, 20) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 14, 23) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 15, 14) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 15, 17) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 15, 20) (push) Waiting to run
Compiler Integration Tests / clang (Debug, 15, 23) (push) Waiting to run
Compiler Integration Tests / clang (Release, 13, 14) (push) Waiting to run
Compiler Integration Tests / clang (Release, 13, 17) (push) Waiting to run
Compiler Integration Tests / clang (Release, 13, 20) (push) Waiting to run
Compiler Integration Tests / clang (Release, 13, 23) (push) Waiting to run
Compiler Integration Tests / clang (Release, 14, 14) (push) Waiting to run
Compiler Integration Tests / clang (Release, 14, 17) (push) Waiting to run
Compiler Integration Tests / clang (Release, 14, 20) (push) Waiting to run
Compiler Integration Tests / clang (Release, 14, 23) (push) Waiting to run
Compiler Integration Tests / clang (Release, 15, 14) (push) Waiting to run
Compiler Integration Tests / clang (Release, 15, 17) (push) Waiting to run
Compiler Integration Tests / clang (Release, 15, 20) (push) Waiting to run
Compiler Integration Tests / clang (Release, 15, 23) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 14, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 17, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 20, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 23, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 14, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 17, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 20, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 23, 14.3.1) (push) Waiting to run
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Waiting to run
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Waiting to run
CI_iOS / iOS (push) Waiting to run
83 lines
3.3 KiB
C++
83 lines
3.3 KiB
C++
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
//
|
|
// This code is licensed under the MIT License (MIT).
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef GSL_NARROW_H
|
|
#define GSL_NARROW_H
|
|
#include "assert" // for GSL_SUPPRESS
|
|
#include "util" // for narrow_cast
|
|
#include <exception> // for std::exception
|
|
namespace gsl
|
|
{
|
|
struct narrowing_error : public std::exception
|
|
{
|
|
const char* what() const noexcept override { return "narrowing_error"; }
|
|
};
|
|
|
|
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
|
template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
|
|
// clang-format off
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
|
GSL_SUPPRESS(es.46) // NO-FORMAT: attribute // The warning suggests that a floating->unsigned conversion can occur
|
|
// in the static_cast below, and that gsl::narrow should be used instead.
|
|
// Suppress this warning, since gsl::narrow is defined in terms of
|
|
// static_cast
|
|
// clang-format on
|
|
constexpr T narrow(U u)
|
|
{
|
|
constexpr const bool is_different_signedness =
|
|
(std::is_signed<T>::value != std::is_signed<U>::value);
|
|
|
|
GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
|
|
GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
|
|
GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
|
|
const T t = narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type
|
|
// and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
|
|
// that we target (i.e., no hardware trap representations are hit).
|
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
|
#endif
|
|
// Note: NaN will always throw, since NaN != NaN
|
|
if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
|
|
{
|
|
throw narrowing_error{};
|
|
}
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
#pragma GCC diagnostic pop
|
|
#endif
|
|
|
|
return t;
|
|
}
|
|
|
|
template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
|
|
// clang-format off
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
|
// clang-format on
|
|
constexpr T narrow(U u)
|
|
{
|
|
const T t = narrow_cast<T>(u);
|
|
|
|
if (static_cast<U>(t) != u)
|
|
{
|
|
throw narrowing_error{};
|
|
}
|
|
|
|
return t;
|
|
}
|
|
} // namespace gsl
|
|
#endif // GSL_NARROW_H
|