ETLCPP / etl

Embedded Template Library
https://www.etlcpp.com
MIT License
2.05k stars 372 forks source link

In subspan function, add static checks on extents #843

Closed mike919192 closed 4 months ago

mike919192 commented 4 months ago

Potential fix for issue #842

etl::span subspan function does not currently statically check extents. std::span does check this.

Code demonstration:

std::array<int, 10> array {0};

//std::span std_span1 = std::span(array).subspan<15, 1>(); //compile time error
//std::span std_span2 = std::span(array).subspan<5, 8>(); //compile time error

etl::span etl_span1 = etl::span(array).subspan<15, 1>(); //currently no compile time error
etl::span etl_span2 = etl::span(array).subspan<5, 8>(); //currently no compile time error

This PR should enable c++11 and newer to have compile error when the extent check fails.

semanticdiff-com[bot] commented 4 months ago

Review changes with SemanticDiff.

mike919192 commented 4 months ago

I also realized that the first and last functions also behave similarly, so I have also added the checks for those functions. I believe those are all the functions where static checks are possible.

std::array<int, 10> array {0};

//std::span std_span1 = std::span(array).first<15>(); //compile time error
//std::span std_span2 = std::span(array).last<15>(); //compile time error

etl::span etl_span1 = etl::span(array).first<15>(); //currently no compile time error
etl::span etl_span2 = etl::span(array).last<15>(); //currently no compile time error

I don't know if it makes sense to also add the static assert for the pre c++11 subspan function? I'll await @jwellbelove feedback.

jwellbelove commented 4 months ago

The ETL_STATIC_ASSERT will work for pre-C++11 as the macro defines a C++03 static assert implementation.

mike919192 commented 4 months ago

Thanks. I duplicated the static asserts for the pre c++11 subspan function. I should be finished with changes unless there are other issues.