Skip to content

string_span static array constexpr constructor not actually constexpr #539

Description

When trying to make a constexpr string_span from a static array, like so:

constexpr gsl::string_span<> str {"asdf"};

VS 2017 gives the errors:

error C2131: expression did not evaluate to a constant
note: failure was caused by call of undefined function or one not declared 'constexpr'
note: see usage of 'gsl::basic_string_span<const char,-1>::remove_z'
note: while evaluating 'gsl::basic_string_span<const char,-1>::basic_string_span(&span, &{97,115,100,102,0})'
error C2131: expression did not evaluate to a constant
note: failure was caused by call of undefined function or one not declared 'constexpr'
note: see usage of 'gsl::basic_string_span<const char,-1>::remove_z'

The constructor called here says:

// From static arrays - if 0-terminated, remove 0 from the view
// All other containers allow 0s within the length, so we do not remove them
template <std::size_t N>
constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
{
}

Since all we really want to do is remove the trailing zero from string literals, it seems like a simpler constructor like so might do what we want and allow for constexpr string spans:

// From static arrays - if 0-terminated, remove 0 from the view
// All other containers allow 0s within the length, so we do not remove them
template <std::size_t N>
constexpr basic_string_span(element_type (&arr)[N]) : span_(arr, arr[N - 1] ? N : N - 1)
{
}

remove_z could also be fixed to be correctly constexpr, I suppose.

The comment seems to imply that we don't care about embedded nulls, so we don't need to look for the first null on the string, just remove the last one if there is one (string literals always have one). If someone hard-coded a static char array with nulls in it, they'd just have to be aware that a single ending null would be removed, but that's no worse than the current situation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Good First IssueSuitable for newcomersHelp WantedNeeds additional help or expertise

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions