msvc countof ut

This commit is contained in:
mutouyun 2022-03-01 11:50:12 +08:00
parent daa3c30f4c
commit 5538a709c9
2 changed files with 69 additions and 6 deletions

View File

@ -9,6 +9,7 @@
#include <cstddef> // std::size_t #include <cstddef> // std::size_t
#include "libipc/def.h" #include "libipc/def.h"
#include "libipc/utility/generic.h"
LIBIPC_NAMESPACE_BEG_ LIBIPC_NAMESPACE_BEG_
@ -21,14 +22,56 @@ constexpr std::size_t countof(T const (&)[N]) noexcept {
return N; return N;
} }
template <typename C> namespace detail {
constexpr auto countof(C const &c) -> decltype(c.size()) {
return c.size(); template <typename C, typename = void>
} struct countof_trait_has_size {
enum : bool { value = false };
};
template <typename C> template <typename C>
constexpr auto countof(C const &c) -> decltype(c.Size()) { struct countof_trait_has_size<C, void_t<decltype(std::declval<C>().size())>> {
return c.Size(); enum : bool { value = true };
};
template <typename C, typename = void>
struct countof_trait_has_Size {
enum : bool { value = false };
};
template <typename C>
struct countof_trait_has_Size<C, void_t<decltype(std::declval<C>().Size())>> {
enum : bool { value = true };
};
template <typename C, bool = countof_trait_has_size<C>::value
, bool = countof_trait_has_Size<C>::value>
struct countof_trait;
template <typename C>
struct countof_trait<C, true, false> {
constexpr static auto countof(C const &c) noexcept(noexcept(c.size())) {
return c.size();
}
using return_t = decltype(countof_trait::countof(std::declval<C>()));
enum : bool { noexcept_value = noexcept(countof_trait::countof(std::declval<C>())) };
};
template <typename C>
struct countof_trait<C, false, true> {
constexpr static auto countof(C const &c) noexcept(noexcept(c.Size())) {
return c.Size();
}
using return_t = decltype(countof_trait::countof(std::declval<C>()));
enum : bool { noexcept_value = noexcept(countof_trait::countof(std::declval<C>())) };
};
} // namespace detail
template <typename C, typename R = detail::countof_trait<C>>
constexpr auto countof(C const &c) noexcept(R::noexcept_value)
-> typename R::return_t {
return R::countof(c);
} }
LIBIPC_NAMESPACE_END_ LIBIPC_NAMESPACE_END_

View File

@ -0,0 +1,20 @@
/**
* @file src/generic.h
* @author mutouyun (orz@orzz.org)
* @brief Tools for generic programming
* @date 2022-03-01
*/
#pragma once
#include "libipc/def.h"
LIBIPC_NAMESPACE_BEG_
/**
* @brief Utility metafunction that maps a sequence of any types to the type void
* @see https://en.cppreference.com/w/cpp/types/void_t
*/
template <typename...>
using void_t = void;
LIBIPC_NAMESPACE_END_