From f35ebfa1df80960a3ca5cbdf6461ca523bfc3232 Mon Sep 17 00:00:00 2001 From: Michael Bloom Date: Thu, 15 Feb 2024 09:23:34 -0500 Subject: [PATCH] Change to ETL_STATIC_ASSERT. Add static assert for first and last functions --- include/etl/span.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/etl/span.h b/include/etl/span.h index bcdb6a22f..1c40d4f1f 100644 --- a/include/etl/span.h +++ b/include/etl/span.h @@ -42,6 +42,7 @@ SOFTWARE. #include "memory.h" #include "array.h" #include "byte.h" +#include "static_assert.h" #include "private/dynamic_extent.h" @@ -295,6 +296,9 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR etl::span first() const ETL_NOEXCEPT { + //if extent is static, check that original span contains at least COUNT elements + ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? COUNT <= extent : true, "Original span does not contain COUNT elements"); + return etl::span(pbegin, pbegin + COUNT); } @@ -312,6 +316,9 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR etl::span last() const ETL_NOEXCEPT { + //if extent is static, check that original span contains at least COUNT elements + ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? COUNT <= extent : true, "Original span does not contain COUNT elements"); + return etl::span(pbegin + Extent - COUNT, (pbegin + Extent)); } @@ -332,10 +339,10 @@ namespace etl etl::span subspan() const ETL_NOEXCEPT { //if extent is static, check that OFFSET is within the original span - static_assert((extent != etl::dynamic_extent) ? OFFSET <= extent : true); + ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? OFFSET <= extent : true, "OFFSET is not within the original span"); //if count is also static, check that OFFSET + COUNT is within the original span - static_assert((extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (extent - OFFSET) : true); + ETL_STATIC_ASSERT((extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (extent - OFFSET) : true, "OFFSET + COUNT is not within the original span"); return (COUNT == etl::dynamic_extent) ? etl::span(pbegin + OFFSET, (pbegin + Extent)) : etl::span(pbegin + OFFSET, pbegin + OFFSET + COUNT);