mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
msvc countof ut
This commit is contained in:
parent
daa3c30f4c
commit
5538a709c9
@ -9,6 +9,7 @@
|
||||
#include <cstddef> // std::size_t
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libipc/utility/generic.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
|
||||
@ -21,14 +22,56 @@ constexpr std::size_t countof(T const (&)[N]) noexcept {
|
||||
return N;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
constexpr auto countof(C const &c) -> decltype(c.size()) {
|
||||
return c.size();
|
||||
}
|
||||
namespace detail {
|
||||
|
||||
template <typename C, typename = void>
|
||||
struct countof_trait_has_size {
|
||||
enum : bool { value = false };
|
||||
};
|
||||
|
||||
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())>> {
|
||||
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_
|
||||
|
||||
20
src/libipc/utility/generic.h
Normal file
20
src/libipc/utility/generic.h
Normal 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_
|
||||
Loading…
x
Reference in New Issue
Block a user