User Tools

Site Tools


cplusplus:string_view

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cplusplus:string_view [2022/04/28 22:23] (current)
dblume created
Line 1: Line 1:
 +====== String_view for objects with a const string field ======
  
 +Store a little string in an object which is going to be its name. It can't change its name, so it ought to be const. Prefer not to use std::string - it's heavyweight, and the data ends up on a separate string allocation.
 +
 +
 +<code c++>
 +struct Str {
 +    constexpr Str(std::string_view s) : buf{[&]{
 +        auto buf = std::array<char, 64>{};
 +        std::copy(s.begin(), s.end(), buf.data());
 +        return buf;
 +    }()}, sv{buf.data()} {}
 +
 +    std::array<char, 64> const buf;
 +    std::string_view const sv;
 +};
 +</code>
cplusplus/string_view.txt ยท Last modified: 2022/04/28 22:23 by dblume