我想看看map实现是否支持不完整类型,因为libc似乎支持它们,但stdlibc不支持。
我在这里尝试了SFINAE,但它给出了一个编译错误:
template<template<typename, typename, typename...> typename MapType, typename = void>
struct allows_incomplete : std::false_type {};
struct incomplete_type;
template<template<typename, typename, typename...> typename MapType>
struct allows_incomplete<MapType, std::void_t<decltype(MapType<std::string_view, incomplete_type>{})>> : std::true_type {};
static_assert(allows_incomplete<std::map>::value);
static_assert(!allows_incomplete<std::unordered_map>::value);
1条答案
按热度按时间wb1gzix01#
要检查map是否支持不完整类型,您需要确定这样的map本身是否是完整类型。完整类型和不完整类型之间的唯一区别是不可能获得不完整类型的大小,即不可能获得不完整类型的
sizeof()
。因此,我们可以基于此构造不完整类型的检测器。类似于以下内容:顺便说一句,GCC 11.3似乎有bug,因为即使是
std::is_constructible<std::unordered_map<int, incomplete_type>>::value
也会给出同样的错误,这绝对是不正确的。