5

I was reading the documentation for std::string_view , and I noticed that these were the constructors:

constexpr basic_string_view() noexcept;
constexpr basic_string_view(const basic_string_view& other) noexcept = default;
constexpr basic_string_view(const CharT* s, size_type count);
constexpr basic_string_view(const CharT* s);

Why didn't they introduced this one?

template<std::size_t n>
constexpr basic_string_view(const CharT(&s)[n]) : basic_string_view(s, n) {}

In the majority of cases, it would save a call to strlen(). Is there a reason it hasn't been introduced?

Barry
  • 286,269
  • 29
  • 621
  • 977
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • 5
    I imagine [this may have something to do with it](https://stackoverflow.com/questions/28243371/why-does-pointer-decay-take-priority-over-a-deduced-template). – StoryTeller - Unslander Monica Nov 16 '17 at 19:10
  • ^^ I'm tempted to close as a dupe, YMMV. – Barry Nov 16 '17 at 19:11
  • 1
    @Barry - I'm amazed I managed to link to it faster than you, tbh :) – StoryTeller - Unslander Monica Nov 16 '17 at 19:12
  • @StoryTeller Oh I see. Adding it would be non trivial (lot of sfinae). I think closing it as a dupe might appropriated. – Guillaume Racicot Nov 16 '17 at 19:13
  • *"In the majority of cases, it would save a call to strlen()"* -- It would also have completely different behavior to calling `strlen()`. It would give you a view of the entire array (including any null terminators), instead of stopping before the first terminator. – Benjamin Lindley Nov 16 '17 at 19:16
  • @BenjaminLindley this is also true. But I'd expect `std::string_view{"Hello\0World"}.size()` to return 11, not 5. – Guillaume Racicot Nov 16 '17 at 19:18
  • @GuillaumeRacicot - Considering you have the second overload which accepts a `size_type count` you aren't really being short changed here. It's only *slighthly* more verbose. – StoryTeller - Unslander Monica Nov 16 '17 at 19:20
  • 1
    @GuillaumeRacicot: 11? Or 12? You'd want it to count the explicit `\0` in the middle, but not the implicit one at the end? Anyway, that's not a normal use case, I would suspect. A more normal use case would be to declare an array with more room than you might need, then fill it via a function where you don't know the exact number of characters that will be placed in there (like sprintf). And then you'd want a view on those characters, not the whole array. – Benjamin Lindley Nov 16 '17 at 19:22
  • 4
    In some cases where this would be useful an alternative would be the `string_view` literal: http://en.cppreference.com/w/cpp/string/basic_string_view/operator%22%22sv – mattnewport Nov 16 '17 at 19:26
  • @mattnewport ahh indeed! So using string view literal can be more efficient. Thank you for pointing this! – Guillaume Racicot Nov 16 '17 at 19:28
  • @GuillaumeRacicot yeah, I believe there should be no runtime `strlen()` when using the `string_view` string literal. – mattnewport Nov 16 '17 at 19:56

1 Answers1

5

The reason is that it's not functionally equivalent

char x[255];
sprintf(x, "hello folks");

// oops, sv.size() == 255!
std::string_view sv(x);

The strlen thing isn't an issue, since many compilers "know" the meaning of a call to strlen and replace it with a constant, if the argument is constant (after inlining the string_view constructor, the argument becomes a string literal. So std::string_view sv("hello folks") will be efficient).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212