diff --git a/misc/traits.h b/misc/traits.h index 2e9e63c..9b41bae 100644 --- a/misc/traits.h +++ b/misc/traits.h @@ -69,6 +69,24 @@ template Bool isIteratableImpl(...); /// \endcond template using IsIteratable = decltype(Detail::isIteratableImpl(0)); + +/// \cond +namespace Detail { +template auto hasSizeImpl(int) -> decltype(std::declval().size(), Bool{}); +template Bool hasSizeImpl(...); +} // namespace Detail +/// \endcond + +template using HasSize = decltype(Detail::hasSizeImpl(0)); + +/// \cond +namespace Detail { +template auto isReservable(int) -> decltype(std::declval().reserve(0u), Bool{}); +template Bool isReservable(...); +} // namespace Detail +/// \endcond + +template using IsReservable = decltype(Detail::isReservable(0)); } // namespace Traits #endif // CPP_UTILITIES_TRAITS_H diff --git a/tests/traitstests.cpp b/tests/traitstests.cpp index 7bad8aa..0b2f15a 100644 --- a/tests/traitstests.cpp +++ b/tests/traitstests.cpp @@ -1,5 +1,6 @@ #include "../misc/traits.h" +#include #include #include #include @@ -13,6 +14,11 @@ struct SomeStruct { int bar; }; +struct CountableStruct { + int numberOfElements; + size_t size() const; +}; + static_assert(!Bool::value, "Bool"); static_assert(Bool::value, "Bool"); static_assert(!Not>::value, "Not"); @@ -31,6 +37,13 @@ static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); +static_assert(!HasSize::value, "HasSize: negative case"); +static_assert(!HasSize>::value, "HasSize: negative case"); +static_assert(HasSize>::value, "HasSize: positive case"); +static_assert(HasSize::value, "HasSize: positive case"); +static_assert(HasSize::value, "HasSize: positive case"); +static_assert(!IsReservable>::value, "HasSize: negative case"); +static_assert(IsReservable>::value, "HasSize: positive case"); static_assert(!IsCString::value, "IsCString: negative case"); static_assert(!IsCString::value, "IsCString: negative case");