#pragma once #include #include #include #include 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 constexpr auto ssize(const C& c) -> std:: common_type_t> { using R = std:: common_type_t>; // 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(c.size())); return static_cast(c.size()); } template // NOLINTNEXTLINE(*-c-arrays) constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t { return N; } } // namespace c10