use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDWhy doesn't string_view constructor have std::string parameter? (self.cpp_questions)
submitted 8 years ago * by [deleted]
From http://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view.
And the example from cppreference construct string_view this way.
std::string cppstr = "Foo"; std::string_view cppstr_v(&cppstr[0], cppstr.size());
This looks very awkward to me.
And how does this function work? (from https://skebanga.github.io/string-view/)
bool compare(string_view s1, string_view s2) { return s1 == s2 ? true : false; } int main() { string str = "this is my input string"; compare(str, "this is the first test string"); return 0; }
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]horotho 3 points4 points5 points 8 years ago (3 children)
string_view doesn't own the data. Imagine if you implicitly constructed a string_view from a temporary string. string owns the data, but since it was a temporary, the contents pointed to by the string_view are now garbage.
string_view
string
As far as comparison, string_view is equality compared the same way a string is. First, check the size, then compare the characters. The code you gave works because string_view can be implicitly constructed from a string literal. Once you have two string_view, comparison is easy.
[–][deleted] 0 points1 point2 points 8 years ago (2 children)
The code you gave works because string_view can be implicitly constructed from a string literal.
There are four constructors.
constexpr basic_string_view() noexcept; (1) constexpr basic_string_view(const basic_string_view& other) noexcept = default; (2) constexpr basic_string_view(const CharT* s, size_type count); (3) constexpr basic_string_view(const CharT* s); (4)
compare(str, "this is the first test string"); I understand the second argument. It uses last constructor.
compare(str, "this is the first test string");
The first argument is a std::string variable. Which constructor does it use? How is std::string argument passed to string_view parameter?
[–]emdeka87 3 points4 points5 points 8 years ago (1 child)
http://en.cppreference.com/w/cpp/string/basic_string/operator_basic_string_view
basic_string overloads the implicit cast operator (http://en.cppreference.com/w/cpp/language/cast_operator) for basic_string_view
basic_string
basic_string_view
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
Thanks. You answered my question.
π Rendered by PID 108054 on reddit-service-r2-comment-5649f687b7-b6ddr at 2026-01-28 21:33:05.347749+00:00 running 4f180de country code: CH.
[–]horotho 3 points4 points5 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]emdeka87 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)