refactor(templates): restrict the return value types of GetData and GetNum

This commit is contained in:
_Redstone_c_ 2023-01-03 08:33:30 +08:00
parent 95b492851a
commit 1deb09480f

View File

@ -7,8 +7,8 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
/** @return The pointer or iterator to the container element storage. */
template <typename T> requires (requires(T&& Container) { Container.GetData(); })
/** @return The pointer to the container element storage. */
template <typename T> requires (requires(T&& Container) { { Container.GetData() } -> CPointer; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{
return Container.GetData();
@ -21,7 +21,7 @@ template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(&
template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(&& Container)[N]) { return Container; }
/** Overloads the GetData algorithm for T::data(). */
template <typename T> requires (requires(T&& Container) { Container.data(); })
template <typename T> requires (requires(T&& Container) { { Container.data() } -> CPointer; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{
return Container.data();
@ -29,13 +29,13 @@ FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
/** Overloads the GetData algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) GetData(initializer_list<T> Container)
FORCEINLINE constexpr const T* GetData(initializer_list<T> Container)
{
return Container.begin();
}
/** @return The number of elements in the container. */
template <typename T> requires (requires(T&& Container) { Container.Num(); })
template <typename T> requires (requires(T&& Container) { { Container.Num() } -> CConvertibleTo<size_t>; })
FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
{
return Container.Num();
@ -48,7 +48,7 @@ template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(& C
template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(&& Container)[N]) { return N; }
/** Overloads the GetNum algorithm for T::size(). */
template <typename T> requires (requires(T&& Container) { Container.size(); })
template <typename T> requires (requires(T&& Container) { { Container.size() } -> CConvertibleTo<size_t>; })
FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
{
return Container.size();
@ -56,7 +56,7 @@ FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
/** Overloads the GetNum algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) GetNum(initializer_list<T> Container)
FORCEINLINE constexpr size_t GetNum(initializer_list<T> Container)
{
return Container.size();
}