Add traits to check for reserve() and size()

experiment/srcref_basic_cfg
Martchus 5 years ago
parent 60e4fead05
commit fa39e2a1aa
  1. 18
      misc/traits.h
  2. 13
      tests/traitstests.cpp

@ -69,6 +69,24 @@ template <typename T> Bool<false> isIteratableImpl(...);
/// \endcond
template <typename T> using IsIteratable = decltype(Detail::isIteratableImpl<T>(0));
/// \cond
namespace Detail {
template <typename T> auto hasSizeImpl(int) -> decltype(std::declval<T &>().size(), Bool<true>{});
template <typename T> Bool<false> hasSizeImpl(...);
} // namespace Detail
/// \endcond
template <typename T> using HasSize = decltype(Detail::hasSizeImpl<T>(0));
/// \cond
namespace Detail {
template <typename T> auto isReservable(int) -> decltype(std::declval<T &>().reserve(0u), Bool<true>{});
template <typename T> Bool<false> isReservable(...);
} // namespace Detail
/// \endcond
template <typename T> using IsReservable = decltype(Detail::isReservable<T>(0));
} // namespace Traits
#endif // CPP_UTILITIES_TRAITS_H

@ -1,5 +1,6 @@
#include "../misc/traits.h"
#include <forward_list>
#include <list>
#include <map>
#include <string>
@ -13,6 +14,11 @@ struct SomeStruct {
int bar;
};
struct CountableStruct {
int numberOfElements;
size_t size() const;
};
static_assert(!Bool<false>::value, "Bool<false>");
static_assert(Bool<true>::value, "Bool<true>");
static_assert(!Not<Bool<true>>::value, "Not");
@ -31,6 +37,13 @@ static_assert(IsIteratable<vector<int>>::value, "IsIterator: positive case");
static_assert(IsIteratable<list<string>>::value, "IsIterator: positive case");
static_assert(IsIteratable<map<string, string>>::value, "IsIterator: positive case");
static_assert(IsIteratable<initializer_list<double>>::value, "IsIterator: positive case");
static_assert(!HasSize<SomeStruct>::value, "HasSize: negative case");
static_assert(!HasSize<forward_list<SomeStruct>>::value, "HasSize: negative case");
static_assert(HasSize<vector<SomeStruct>>::value, "HasSize: positive case");
static_assert(HasSize<string>::value, "HasSize: positive case");
static_assert(HasSize<CountableStruct>::value, "HasSize: positive case");
static_assert(!IsReservable<list<SomeStruct>>::value, "HasSize: negative case");
static_assert(IsReservable<vector<SomeStruct>>::value, "HasSize: positive case");
static_assert(!IsCString<string>::value, "IsCString: negative case");
static_assert(!IsCString<int[]>::value, "IsCString: negative case");

Loading…
Cancel
Save