Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions core/templates/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ class Span {
std::is_same<T, wchar_t>>;

_FORCE_INLINE_ constexpr Span() = default;
_FORCE_INLINE_ constexpr Span(const T *p_ptr, uint64_t p_len) :
_ptr(p_ptr), _len(p_len) {}

_FORCE_INLINE_ Span(const T *p_ptr, uint64_t p_len) :
_ptr(p_ptr), _len(p_len) {
#ifdef DEBUG_ENABLED
// TODO In c++20, make this check run only in non-consteval, and make this constructor constexpr.
if (_ptr == nullptr && _len > 0) {
ERR_PRINT("Internal bug, please report: Span was created from nullptr with size > 0. Recovering by using size = 0.");
_len = 0;
}
#endif
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we do this, or is too risky of having things compile in release but not debug? (and vice versa) 🤔

The CI checks should be pretty good at catching any problems as they do editor and template builds.

#ifdef DEBUG_ENABLED
	_FORCE_INLINE_ Span(const T *p_ptr, uint64_t p_len) :
			_ptr(p_ptr), _len(p_len) {
		// TODO In c++20, make this check run only in non-consteval, and make this constructor constexpr.
		if (_ptr == nullptr && _len > 0) {
			ERR_PRINT("Internal bug, please report: Span was created from nullptr with size > 0. Recovering by using size = 0.");
			_len = 0;
		}
	}
#else
	_FORCE_INLINE_ constexpr Span(const T *p_ptr, uint64_t p_len) :
			_ptr(p_ptr), _len(p_len) {}
#endif

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That would work, but i think since it's effectively constexpr anyway, it should be compiled as such with or without the constexpr modifier. The only difference being that we won't be able to use it in constexpr contexts, which is also the case with separate DEBUG and RELEASE definitions.


// Allows creating Span directly from C arrays and string literals.
template <size_t N>
Expand Down
8 changes: 4 additions & 4 deletions tests/core/templates/test_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ TEST_CASE("[Span] Constexpr Validators") {
static_assert(span_empty.is_empty());

constexpr static uint16_t value = 5;
constexpr Span<uint16_t> span_value(&value, 1);
static_assert(span_value.ptr() == &value);
static_assert(span_value.size() == 1);
static_assert(!span_value.is_empty());
Span<uint16_t> span_value(&value, 1);
CHECK(span_value.ptr() == &value);
CHECK(span_value.size() == 1);
CHECK(!span_value.is_empty());

static constexpr int ints[] = { 0, 1, 2, 3, 4, 5 };
constexpr Span<int> span_array = ints;
Expand Down