Skip to content

Conversation

@mike919192
Copy link
Contributor

A small change to the static span constructor from c array to require that Extent is equal to Array_Size. std span on gcc also does this same check.

This prevents the following code from compiling:

int arr[5]{};
etl::span<int, 10> span1(arr);  //currently no compile error here.  equivalent std span on gcc does have compile error

@jwellbelove jwellbelove changed the base branch from master to development March 27, 2025 12:11
/// Construct from C array
//*************************************************************************
template<size_t Array_Size>
template<size_t Array_Size, typename = typename etl::enable_if<(Extent == etl::dynamic_extent) || (Array_Size == Extent), void>::type>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the (Extent == etl::dynamic_extent) test is necessary here, as this class specialisation is for fixed extent only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right! But I was following the style of the other code in the fixed span, where there are also checks for either == or != etl::dynamic_extent. Still I can try removing it in this constructor, I think all the unit tests should still be good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I was following the style of the other code in the fixed span

I'll take a look at the other code.

@jwellbelove jwellbelove changed the base branch from development to pull-request/#1055-Add-enable_if-restriction-for-span-constructor-from-c-array March 27, 2025 15:14
@jwellbelove jwellbelove merged commit a75d1d2 into ETLCPP:pull-request/#1055-Add-enable_if-restriction-for-span-constructor-from-c-array Mar 27, 2025
63 checks passed
@jwellbelove
Copy link
Contributor

I was wondering whether I should add a runtime check for when a dynamic etl::span is used to copy construct to a larger fixed etl::span. In the STL it seems to be classed as just 'undefined behaviour' in release mode.

@mike919192
Copy link
Contributor Author

mike919192 commented Mar 27, 2025

I was wondering whether I should add a runtime check for when a dynamic etl::span is used to copy construct to a larger fixed etl::span. In the STL it seems to be classed as just 'undefined behaviour' in release mode.

I took a look at how how gcc handles this.

std::vector<int> vec {0, 1, 2, 3, 4};
std::span test1(vec);
std::span<int, 10> test2(test1);

In the constructor for test2, there is a debug assert that test2::Extent == test1.size(). So I could see if being reasonable to do something similar with an ETL_ASSERT.

I would hope that I wouldn't run into this case much. Constructing a fixed span from a dynamic just seems like a bad idea, but mistakes can happen.

Edit: Ah, just realized the constructor is noexcept. So one more thing to keep in mind.

jwellbelove pushed a commit that referenced this pull request Mar 28, 2025
…onstructor-from-c-array' into development

# Conflicts:
#	include/etl/span.h
#	test/test_span_fixed_extent.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants