Skip to content

Need help understanding parts of span.h #22289

@ViralTaco

Description

@ViralTaco

Hi, in span.h you define a strange macro

#define CONSTEXPR_IF_NOT_DEBUG

could someone please tell me the reasoning behind this?
It doesn't seem to be mentionned in the comments, maybe I overlooked it.

The reason I don't understand is that it, as far as I know, does not have to change anything whatsoever in terms of code generation.
Static initialization can happen without constexpr. (In fact it's a sneaky way to introduce bugs whenever you have a global std::map… It's a lot of "fun" but I digress).

Then we have this

bitcoin/src/span.h

Lines 116 to 125 in 965e937

/** Construct a span from a begin and end pointer.
*
* This implements a subset of the iterator-based std::span constructor in C++20,
* which is hard to implement without std::address_of.
*/
template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin)
{
ASSERT_IF_DEBUG(end >= begin);
}

Now I'm very confused.
Is this checking if an incomplete type is convertible to another incomplete type?
I don't know how that template could possibly do anything else. It's not checking if T** is implicitly convertible to C**, is it? I don't know what that does.

Why is it only constexpr when not in debug?
Why is it only doing this, rather important looking, assertion in debug?
Does it become valid to violate that assertion in prod?
Why? It seems odd and bug prone.

Thank you for your time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions