string-view

Why string_view instead of generalized container_view<T>?

别说谁变了你拦得住时间么 提交于 2019-12-01 02:36:32
I've found string_view from new C++17 standard a bit redundant. We've got a quite verbose collection of simple mechanisms for passing data to callee , without much overhead and now there is one another which is also specific only to one container type. I don't understand why providing this machinery only for string and not some more generalized type for other containers. One sensible answer is that we've already had these kinds of solutions. For example in C++17 and beyond presentation string_view is explained as observer_ptr<T> (or T*) for string . Please state the arguments against a more

parsing from std::string into a boost::string_view using boost::spirit::x3

心不动则不痛 提交于 2019-12-01 00:57:43
In my my previous question it was suggested that the performance of my boost::spirit::x3 parser could be improved by parsing into a boost::string_view using the raw directive. However, I have difficulties getting it to compile. This is what I found out: Before x3 , one had to specialize assign_to_attribute_from_iterators (see e.g. this SO answer ) to handle the raw directive. x3 now uses the move_to free function instead (see e.g. this SO answer ). I therefore added a move_to overload which works if I parse from char* : #include <iostream> #include <string> #include <boost/utility/string_view

Conversion from std::string_view to std::string is implicit. What the hell did committe think?

我只是一个虾纸丫 提交于 2019-11-30 17:30:57
Seriously, whats't up. There is an implicit conversion from std::string to std::string_view and it's not considered unsafe. Even though this surely may cause a lot of dangling references if programmer is not careful. On the other hand, they have dismissed an implicit conversion from std::string_view to std::string using same argument but in completely opposite fashion: because programmer may be not careful . It's lovely that they have created a replacement for a raw const char* pointer while making it super confusing and stripped to the bone: Implicit const char* -> std::string : OK Implicit

Use of string_view for map lookup

寵の児 提交于 2019-11-30 07:19:41
问题 The following code fails to build on recent compilers (g++-5.3, clang++-3.7). #include <map> #include <functional> #include <experimental/string_view> void f() { using namespace std; using namespace std::experimental; map<string, int> m; string s = "foo"; string_view sv(s); m.find(sv); } Error returned by clang : error: no matching member function for call to 'find' m.find(sv); ~~^~~~ But shouldn't find be able to use comparable types ? Cppreference mentions the following overload : template<

Conversion from std::string_view to std::string is not implicit. What the hell was the committee thinking?

和自甴很熟 提交于 2019-11-30 01:24:42
问题 Seriously, whats't up. There is an implicit conversion from std::string to std::string_view and it's not considered unsafe. Even though this surely may cause a lot of dangling references if programmer is not careful. On the other hand, they have dismissed an implicit conversion from std::string_view to std::string using same argument but in completely opposite fashion: because programmer may be not careful . It's lovely that they have created a replacement for a raw const char* pointer while

Use of string_view for map lookup

折月煮酒 提交于 2019-11-29 02:56:21
The following code fails to build on recent compilers (g++-5.3, clang++-3.7). #include <map> #include <functional> #include <experimental/string_view> void f() { using namespace std; using namespace std::experimental; map<string, int> m; string s = "foo"; string_view sv(s); m.find(sv); } Error returned by clang : error: no matching member function for call to 'find' m.find(sv); ~~^~~~ But shouldn't find be able to use comparable types ? Cppreference mentions the following overload : template< class K > iterator find( const K& x ); The same error happens with boost::string_ref . You need to

Why is there no support for concatenating std::string and std::string_view?

空扰寡人 提交于 2019-11-28 06:44:20
Since C++1z, we have std::string_view , a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use std::string_view . However, one quickly finds out that switching from const std::string& to std::string_view breaks code that uses string concatenation as there is no support for concatenating std::string and std::string_view : std::string{"abc"} + std::string_view{"def"}; // ill-formed (fails to compile) std::string_view{"abc"} + std::string{"def"}; // ill-formed (fails

How exactly is std::string_view faster than const std::string&?

雨燕双飞 提交于 2019-11-27 09:03:52
问题 std::string_view has made it to C++17 and it is widely recommended to use it instead of const std::string& . One of the reasons is performance. Can someone explain how exactly std::string_view is/will be faster than const std::string& when used as a parameter type? (let's assume no copies in the callee are made) 回答1: std::string_view is faster in a few cases. First, std::string const& requires the data to be in a std::string , and not a raw C array, a char const* returned by a C API, a std:

What is string_view?

眉间皱痕 提交于 2019-11-27 02:59:42
string_view was a proposed feature within the C++ Library Fundamentals TS( N3921 ) added to C++17 As far as i understand it is a type that represent some kind of string "concept" that is a view of any type of container that could store something viewable as a string. Is this right ? Should the canonical const std::string& parameter type become string_view ? Is there another important point about string_view to take into consideration ? The purpose of any and all kinds of "string reference" and "array reference" proposals is to avoid copying data which is already owned somewhere else and of

Why is there no support for concatenating std::string and std::string_view?

做~自己de王妃 提交于 2019-11-27 01:27:51
问题 Since C++1z, we have std::string_view, a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use std::string_view . However, one quickly finds out that switching from const std::string& to std::string_view breaks code that uses string concatenation as there is no support for concatenating std::string and std::string_view : std::string{"abc"} + std::string_view{"def"