string-view

Should methods returning const std::string& return const std::string_view instead?

删除回忆录丶 提交于 2019-12-06 19:27:26
问题 Assume we have a simple getter method in a class that returns a const reference to a std::string member: const std::string& getString() const noexcept { return someString; } With the advent of std::string_view in C++17, I wonder whether it has any advantages of writing this instead: const std::string_view getString() const noexcept { return someString; } Does one method have advantages/disadvantages over the other? Clearly (correct me if I'm wrong) both solutions will definitely be better

How do I make a constexpr swap function?

对着背影说爱祢 提交于 2019-12-06 13:57:27
I'm making my own String View class for learning purposes, and I'm trying to make it 100% constexpr. To test it, I have a member function that returns an hash value. I then construct my string view in a switch statement and call that same member function, if it passes, that member function has fullfiled its purpose. To learn, I'm using / reading / comparing my implementation with Visual Studio 2017 latest update std::string_view , however, I've noticed that, despite swap being marked as constexpr , it does not work, nor in Visual Studio, nor in g++. This is the piece of code that does not work

Is there a safe way to assert if a string view is null terminated?

寵の児 提交于 2019-12-06 07:50:13
I have some part in my code that uses string view extensively. It would be inconceivable to use std::string everywhere, and char const* won't work since there is associative containers, many comparisons and such operations that are hard to do with plain raw strings. However, there is a place that will eventually deal with a C API, and it needs null terminated strings: auto sv = std::string_view{/* ... */}; c_api(sv.data()); Although this works fine in my case, I'd like to be sure everything is okay and assert that the strings are null terminated, as my system constructing the string views and

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

纵然是瞬间 提交于 2019-12-06 02:46:35
问题 Trying to provide a solution to std::string_view and std::string in std::unordered_set, I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::string_view, std::unique_ptr<std::string>> (the value is std::unique_ptr<std::string> because the small string optimization would mean that the address of the string 's underlying data will not always be transferred as a result of std::move . My original test code, that seems to work, is (omitting headers): using

How you convert a std::string_view to a const char*?

痞子三分冷 提交于 2019-12-04 23:51:40
Compiling with gcc-7.1 with the flag -std=c++17 , the following program raises an error: #include <string_view> void foo(const char* cstr) {} void bar(std::string_view str){ foo(str); } The error message is In function 'void bar(std::string_view)': error: cannot convert 'std::string_view {aka std::basic_string_view<char>}' to 'const char*' for argument '1' to 'void foo(const char*)' foo(str); I'm surprised there is no conversion to const char* because other libraries (abseil, bde), provide similar string_view classes which implicitly convert to const char* . A std::string_view doesn't provide

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

百般思念 提交于 2019-12-04 09:18:58
Trying to provide a solution to std::string_view and std::string in std::unordered_set , I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::string_view, std::unique_ptr<std::string>> (the value is std::unique_ptr<std::string> because the small string optimization would mean that the address of the string 's underlying data will not always be transferred as a result of std::move . My original test code, that seems to work, is (omitting headers): using namespace std::literals; int main(int argc, char **argv) { std::unordered_map<std::string_view, std:

string_view behaviour when passing temporary std::string

社会主义新天地 提交于 2019-12-04 00:24:35
问题 I just ran into some misunderstanding: at least in libc++ implementation std::experimental::string_view has the following concise implementation: template <class _CharT, class _Traits....> class basic_string_view { public: typedef _CharT value_type; ... template <class _Allocator> basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& str): __data(str.data()), __size(str.size()) { } private: const value_type* __data; size_type __size; }; Does this implementation imply that if we

Why does std::string_view create a dangling view in a ternary expression?

こ雲淡風輕ζ 提交于 2019-12-03 11:15:15
问题 Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling string view: const std::string& otherMethod(); std::string_view myMethod(bool bla) { return bla ? otherMethod() : ""; // Dangling view! } https://godbolt.org/z/1Hu_p2 It seems that the compiler first puts a temporary std::string copy of the result of otherMethod() on the stack and then returns a view

When would I pass const& std::string instead of std::string_view?

自闭症网瘾萝莉.ら 提交于 2019-12-03 09:57:09
I understand the motivation for using std::string_view ; it can help avoid unecessary allocations in function arguments. For example: The following program will create a std::string from a string literal. This causes an undesired dynamic allocation, as we are only interested observing the characters. #include <iostream> void* operator new(std::size_t n) { std::cout << "[allocating " << n << " bytes]\n"; return malloc(n); } void observe_string(std::string const& str){} int main(){ observe_string("hello world"); //prints [allocating 36 bytes] } Using string_view will solve the problem: #include

Why does std::string_view create a dangling view in a ternary expression?

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:35:13
Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling string view: const std::string& otherMethod(); std::string_view myMethod(bool bla) { return bla ? otherMethod() : ""; // Dangling view! } https://godbolt.org/z/1Hu_p2 It seems that the compiler first puts a temporary std::string copy of the result of otherMethod() on the stack and then returns a view of this temporary copy instead of just returning a view of the reference. First I thought about a