Sha256: f33e6f4da32313ad83e7e7995e1fed22c614dc1c187f9c46740c36a73be4531e

Contents?: true

Size: 1.72 KB

Versions: 10

Compression:

Stored size: 1.72 KB

Contents

///////////////////////////////////////////////////////////////////////////////
//
// 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 <gsl/gsl_assert> // for Expects
#include <gsl/gsl_util> // for narrow_cast
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>
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
constexpr
T narrow(U u) noexcept(false)
{
    constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value);

    const T t = narrow_cast<T>(u);

    if (static_cast<U>(t) != u
        || (is_different_signedness
            && ((t < T{}) != (u < U{}))))
    {
        throw narrowing_error{};
    }

    return t;
}
}
#endif // GSL_NARROW_H

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
couchbase-3.2.0-universal-darwin-20 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.2.0 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.1.1 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.1.1-universal-darwin-20 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.1.0 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.0.3 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.0.3-universal-darwin-20 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.0.2 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.0.1-universal-darwin-19 ext/third_party/gsl/include/gsl/gsl_narrow
couchbase-3.0.1 ext/third_party/gsl/include/gsl/gsl_narrow