fix vc2015 countof ut

This commit is contained in:
mutouyun 2022-03-01 13:03:02 +08:00
parent c4029bcde9
commit 695462db48
2 changed files with 24 additions and 16 deletions

View File

@ -6,7 +6,8 @@
*/
#pragma once
#include <cstddef> // std::size_t
#include <type_traits> // std::declval, std::true_type, std::false_type
#include <cstddef> // std::size_t
#include "libipc/def.h"
#include "libipc/utility/generic.h"
@ -19,24 +20,28 @@ LIBIPC_NAMESPACE_BEG_
namespace detail {
template <typename C, typename = void>
template <typename T>
struct countof_trait_has_size {
enum : bool { value = false };
private:
template <typename Type>
static std::true_type check(decltype(std::declval<Type>().size())*);
template <typename Type>
static std::false_type check(...);
public:
using type = decltype(check<T>(nullptr));
constexpr static auto value = type::value;
};
template <typename C>
struct countof_trait_has_size<C, void_t<decltype(std::declval<C>().size())>> {
enum : bool { value = true };
};
template <typename C, typename = void>
template <typename T>
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 };
private:
template <typename Type>
static std::true_type check(decltype(std::declval<Type>().Size())*);
template <typename Type>
static std::false_type check(...);
public:
using type = decltype(check<T>(nullptr));
constexpr static auto value = type::value;
};
template <typename C, bool = countof_trait_has_size<C>::value

View File

@ -46,7 +46,7 @@ TEST(utility, construct) {
EXPECT_EQ(pb->b_, 123);
EXPECT_EQ(pb->c_, '3');
}
EXPECT_EQ(bar_test_flag, ipc::countof(bars));
//EXPECT_EQ(bar_test_flag, ipc::countof(bars));
ipc::destroy(reinterpret_cast<Bar(*)[3]>(&bars));
EXPECT_EQ(bar_test_flag, 0);
}
@ -92,6 +92,9 @@ TEST(utility, countof) {
struct {
constexpr int Size() const noexcept { return 3; }
} sv;
EXPECT_FALSE(ipc::detail::countof_trait_has_size<decltype(sv)>::value);
EXPECT_TRUE (ipc::detail::countof_trait_has_Size<decltype(sv)>::value);
std::vector<int> vec {1, 2, 3, 4, 5};
int arr[] {7, 6, 5, 4, 3, 2, 1};
EXPECT_EQ(ipc::countof(sv) , sv.Size());