1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#pragma once
#include <c10/util/Exception.h>
#include <c10/util/TypeSafeSignMath.h>
#include <cstddef>
#include <type_traits>
namespace c10 {
// Implementations of std::ssize() from C++ 20.
//
// This is useful in particular for avoiding -Werror=sign-compare
// issues.
//
// Use this with argument-dependent lookup, e.g.:
// use c10::ssize;
// auto size = ssize(container);
//
// As with the standard library version, containers are permitted to
// specialize this with a free function defined in the same namespace.
//
// See https://en.cppreference.com/w/cpp/iterator/size for more
// information as well as the source of our implementations.
//
// We augment the implementation by adding an assert() if an overflow
// would occur.
template <typename C>
constexpr auto ssize(const C& c) -> std::
common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> {
using R = std::
common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
// We expect this to be exceedingly rare to fire and don't wish to
// pay a performance hit in release mode.
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!greater_than_max<R>(c.size()));
return static_cast<R>(c.size());
}
template <typename T, std::ptrdiff_t N>
// NOLINTNEXTLINE(*-c-arrays)
constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t {
return N;
}
} // namespace c10
|