Search code examples
c++constexprstring-viewconstant-expression

how to construct a constexpr static_string from constexpr std::string_view


I have a static_string template

template <std::size_t N>
struct static_string {
    std::array<char, N + 1> elements_; 

    constexpr static_string() noexcept : elements_{} {}

    constexpr static_string(const char (&data)[N + 1]) noexcept {
        for (size_t i = 0; i < N + 1; i++) {
            elements_[i] = data[i];
        }
    }
};

template <std::size_t N>
static_string(const char (&)[N]) -> static_string<N - 1>;

It works fine when use constexpr static_string str1("this is a str");

And I want to construct from a constexpr std::string_view, so I add:

    constexpr static_string(std::string_view sv) noexcept {
        for (size_t i = 0; i < N; ++i) {
            elements_[i] = sv[i];
        }
    }

then if I use

   constexpr std::string_view sv = "this is a str";
   constexpr static_string<sv.size()> str2(sv);

the compiler outputs

<source>: In function 'int main()':
<source>:33:47: error: 'static_string<13>{std::array<char, 14>{std::__array_traits<char, 14>::_Type{'t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r'}}}' is not a constant expression
   33 |     constexpr static_string<sv.size()> str2(sv);
      |                                               ^
<source>:33:47: error: 'static_string<13>(sv)' is not a constant expression because it refers to an incompletely initialized variable
Compiler returned: 1

So why this expression did not evaluate to a constant, is it possible to construct from constexpr std::string_view? thanks, see https://www.godbolt.org/z/6TToEPnch


Solution

  • The error message issued by the compiler is misleading.

    In fact, the issue is not related to the input string_view but to the fact that elements_ is not fully initialized, which is not allowed in a constexpr constructor
    <ref needed
    perhaps https://timsong-cpp.github.io/cppwp/n4861/expr.const#11.4

    A constant expression is... if the value is an object of class or array type, each subobject satisfies these constraints for the value.

    >.

    To fix that, just add the missing null terminating character:

    #include <array>
    #include <iostream>
    #include <string_view>
    
    template <std::size_t N>
    struct static_string {
        std::array<char, N + 1> elements_;
    
        constexpr static_string() noexcept : elements_{} {}
    
        constexpr static_string(const char (&data)[N + 1]) noexcept {
            for (size_t i = 0; i < N + 1; i++) {
                elements_[i] = data[i];
            }
        }
    
        constexpr static_string(const std::string_view& sv) noexcept {
            for (size_t i = 0; i < N; ++i) {
                elements_[i] = sv[i];
            }
            elements_[N] = '\0';  // completing the initialization of the object
        }
    };
    
    template <std::size_t N>
    static_string(const char (&)[N]) -> static_string<N - 1>;
    
    int main() {
        constexpr std::string_view sv = "this is a str";
        constexpr static_string str1("this is a str");
        constexpr static_string<sv.size()> str2(sv);
    }
    

    LIVE

    (btw, you can as well pass the string_view by const reference)