Questions tagged [string-view]

A string_view is a C++ class template that is a non-owning reference to a string.

Reference: http://en.cppreference.com/w/cpp/experimental/basic_string_view

215 questions
1
vote
1 answer

How to guarantee a specific set of characters as input to a string_view parameter?

I'm working on a design implementation where I want the input to be selected from a specific set of characters for its string literal representation. Consider the following set of classes: class enum BaseType { BINARY = 2, OCTAL = 8, …
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
1
vote
2 answers

Why does string_view::operator== accepts parameters by value

I was reading source code of string_view, and found that operator== accepts parameters by value. template constexpr bool operator==(basic_string_view<_CharT, _Traits> __x, …
Jouni
  • 321
  • 1
  • 8
1
vote
1 answer

How to call a constexpr function from a preprocessor #if directive?

I want to define a macro as a string and later at compile time include code based on string comparison: #include #include constexpr bool strings_equal(char const * a, char const * b) { return…
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
1
vote
1 answer

constexpr C++ container – a way to push_back to initializer_list or similar

This function parses input line into argument like shell(bash,ksh,fish) does. I.e. looks in input string parts separated by whitespaces or tabs: auto parse_args(string_view const& line){ vector args; size_t pos_begin = 0,…
kyb
  • 7,233
  • 5
  • 52
  • 105
1
vote
1 answer

Returning a std::string_view to a const char *

So, I was reading about using enum as part of the new C++2a standards here and came across the following code: enum class rgba_color_channel { red, green, blue, alpha}; std::string_view to_string(rgba_color_channel channel) { switch (my_channel)…
davidbear
  • 375
  • 2
  • 13
1
vote
1 answer

why std::string_view is not trivial?

I want to "migrate" to C++17 and am researching it. I found that this: https://gcc.godbolt.org/z/sPnsEM #include #include int main(){ return (std::is_standard_layout_v ? 10 : 20) + …
Nick
  • 9,962
  • 4
  • 42
  • 80
1
vote
1 answer

Is libc++ providing hash specialization for too many basic_string_view's?

Per [string.view.synop]: // ... // [string.view.hash], hash support template struct hash; template<> struct hash; template<> struct hash; template<> struct hash; template<> struct…
L. F.
  • 19,445
  • 8
  • 48
  • 82
1
vote
2 answers

hash vs hash

C++17 introduced hash. When writing my own custom hashes I want to reuse the STL implementation of the string hash. Is std::hash()("Hello, world") slower than std::hash()("Hello, world")? Is it the…
Valentin
  • 1,108
  • 8
  • 18
1
vote
2 answers

Boost.Container flat_map and std::string_view

For some time I’ve used Boost’s flat_map as my go-to associative collection, for the reasons explained cited on their documentation intro, and (originally) the fact that it gave newer features before the compiler’s std implementation, and it was the…
JDługosz
  • 5,592
  • 3
  • 24
  • 45
1
vote
1 answer

Constexpr string_view comparison

I have a small program that compiles on GCC but not on MSVCwhich compiler isn't following the standard for constexpr string_view comparison? #include #include int main(int argc, char **argv) { const constexpr auto a =…
Bomaz
  • 1,871
  • 1
  • 17
  • 22
1
vote
1 answer

Read whole text file into memory, then process it line-by-line without allocation/copy

Suppose we've read the content of a text file into a stringstream via std::ifstream file(filepath); std::stringstream ss; ss << file.rdbuf(); and now want to process the file line-by-line. This can be done via for (std::string line;…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1
vote
0 answers

Converting string_view to null terminated c string

How would you convert a std::string_view to a null-terminated c string? I've passed a string_view into a function, and need to pass that into another function, which takes a const char* parameter. However, looking at this documentation for…
Jeffmagma
  • 452
  • 2
  • 8
  • 20
1
vote
0 answers

Why does string_view passed into constexpr function compile when passed as template type?

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)…
user409697
  • 71
  • 4
1
vote
1 answer

Passing xvalue of std::string to function taking std::string_view

Is it UB to do the following? void foo(std::string_view view) {...} void bar() { std::string str; foo(std::move(str)); } Thanks!
Bikineev
  • 1,685
  • 15
  • 20
1
vote
1 answer

Differences in size of std::string_view of non-null terminated char array

I was playing around with std::string_view with different compilers and noticed that each compiler prints out different sizes when initializing the std::string_view with a non-null terminated char array. It seems that every compiler prints out the…
JayPhi
  • 361
  • 3
  • 7