c++ 如何检查::testing::Types模板的大小?

ghg1uchk  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在创建一个TYPED_TEST_SUITE,我想获取::testing::Types中元素的计数,原因是要执行静态Assert,这样我就可以确保我的测试是最新的源代码。
对于std::tuple,可以执行以下操作:

int count = tuple_size<decltype(mytuple)>::value

有没有可能对gtests的testing::Types做同样的事情?或者在编译时把std::tuple转换成testing::Types?

owfi6suc

owfi6suc1#

是的。你可以这样做:

template<typename...>
struct CountTypes;
template<typename... Args>
struct CountTypes<::testing::Types<Args...>> : std::integral_constant<int, sizeof...(Args)> {};

template<typename...>
struct ConvertTypes;
template<typename... Args>
struct ConvertTypes<::testing::Types<Args...>> { using type = std::tuple<Args...>;};

int main()
{
    using MyTypes = ::testing::Types<char, int>;
    static_assert(CountTypes<MyTypes>::value == 2);
    static_assert(std::is_same_v<ConvertTypes<MyTypes>::type, std::tuple<char, int>>);
    std::cout << CountTypes<MyTypes>::value << std::endl;
}

相关问题