1

This code does not compile. I assume this is because string_view is not a LiteralType, which violates the constexpr function conditions (http://en.cppreference.com/w/cpp/language/constexpr):

constexpr std::size_t find_space(std::string_view sv) noexcept {
    return sv.find(' ');
}

int main() {
    const std::string_view sv("Finding first space");
    return find_space(sv);
}

However... if I replace with a template value it compiles fine. Why does the compiler allow this?

template <typename STRING_VIEW>
constexpr std::size_t find_space(STRING_VIEW sv) noexcept {
    return sv.find(' ');
}

It's as if the compiler is ignoring the constexpr, or somehow STRING_VIEWs traits are legitimately allowing it to pass.

Using GCC 7.1 with -O3 -std=c++1z. Sandbox code at https://godbolt.org/g/z89Myb

Many thanks, Alex

user409697
  • 71
  • 4

0 Answers0