From 695462db48339134899cdcb495939362c7282834 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Tue, 1 Mar 2022 13:03:02 +0800 Subject: [PATCH] fix vc2015 countof ut --- src/libipc/utility/countof.h | 35 ++++++++++++++++++++--------------- test/test_utility.cpp | 5 ++++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/libipc/utility/countof.h b/src/libipc/utility/countof.h index 39f6999..8685e84 100644 --- a/src/libipc/utility/countof.h +++ b/src/libipc/utility/countof.h @@ -6,7 +6,8 @@ */ #pragma once -#include // std::size_t +#include // std::declval, std::true_type, std::false_type +#include // std::size_t #include "libipc/def.h" #include "libipc/utility/generic.h" @@ -19,24 +20,28 @@ LIBIPC_NAMESPACE_BEG_ namespace detail { -template +template struct countof_trait_has_size { - enum : bool { value = false }; +private: + template + static std::true_type check(decltype(std::declval().size())*); + template + static std::false_type check(...); +public: + using type = decltype(check(nullptr)); + constexpr static auto value = type::value; }; -template -struct countof_trait_has_size().size())>> { - enum : bool { value = true }; -}; - -template +template struct countof_trait_has_Size { - enum : bool { value = false }; -}; - -template -struct countof_trait_has_Size().Size())>> { - enum : bool { value = true }; +private: + template + static std::true_type check(decltype(std::declval().Size())*); + template + static std::false_type check(...); +public: + using type = decltype(check(nullptr)); + constexpr static auto value = type::value; }; template ::value diff --git a/test/test_utility.cpp b/test/test_utility.cpp index 6f4d47c..a2e9d8e 100644 --- a/test/test_utility.cpp +++ b/test/test_utility.cpp @@ -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(&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::value); + EXPECT_TRUE (ipc::detail::countof_trait_has_Size::value); + std::vector vec {1, 2, 3, 4, 5}; int arr[] {7, 6, 5, 4, 3, 2, 1}; EXPECT_EQ(ipc::countof(sv) , sv.Size());