<分区>
我正在尝试创建一种可以检测的“包装特征”,对于某些 T
和一些现有的一元特征 Concept
,即:
-
T
是“可迭代的”,并且 -
T::value_type
满足Concept
这很有用,因为我有其他代码可能需要各种类型,包括 std::vector<various types>
,我想将此包装器特征用于 enable_if
所述代码中的各种功能。
这是一个草图:
#include <type_traits>
#include <vector>
template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::declval<T&>().begin() == std::declval<T&>().end())>> : std::true_type {};
template <class T>
constexpr bool is_iterable_v = is_iterable<T>::value;
template <typename T, typename = void>
struct iterable_value_type
{
using type = std::false_type;
};
template <typename T>
struct iterable_value_type<T, std::void_t<decltype(T::value_type)>>
{
using type = typename T::value_type;
};
template <class T>
using iterable_value_type_t = typename iterable_value_type<T>::type;
// Transforms a concept "does type T have property P" into the concept
// "is type T1 an iterable with value_type T2 where T2 has property P"
template <typename T, template <typename...> typename BaseConcept>
struct CollectionConcept
{
static constexpr bool value = is_iterable_v<T> && BaseConcept<iterable_value_type_t<T>>::value;
};
int main()
{
static_assert(std::is_arithmetic<int>::value);
static_assert(CollectionConcept<std::vector<int>, std::is_arithmetic>::value);
}
不幸的是第二个static_assert
触发器,我不知道为什么。
我该如何实现?