string-view

Safely convert std::string_view to int (like stoi or atoi)

笑着哭i 提交于 2020-04-13 03:24:14
问题 Is there a safe standard way to convert std::string_view to int ? Since C++11 std::string lets us use stoi to convert to int : std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23 try { int i3 = stoi(std::string("abc")); } catch(const std::exception& e) { std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument' } But stoi does not support std::string_view . So alternatively, we could use atoi ,

Safely convert std::string_view to int (like stoi or atoi)

心不动则不痛 提交于 2020-04-13 03:23:28
问题 Is there a safe standard way to convert std::string_view to int ? Since C++11 std::string lets us use stoi to convert to int : std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23 try { int i3 = stoi(std::string("abc")); } catch(const std::exception& e) { std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument' } But stoi does not support std::string_view . So alternatively, we could use atoi ,

How do I make a constexpr swap function?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 00:31:48
问题 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

Unpack parameter pack into string view

 ̄綄美尐妖づ 提交于 2019-12-23 18:04:05
问题 It is possible to unpack a value template parameter pack of type char into a (compile time) string. How does one acquire a string_view into that string? What I want to do: int main() { constexpr auto s = stringify<'a', 'b', 'c'>(); constexpr std::string_view sv{ s.begin(), s.size() }; return 0; } Try: template<char ... chars> constexpr auto stringify() { std::array<char, sizeof...(chars)> array = { chars... }; return array; } Error: 15 : <source>:15:30: error: constexpr variable 'sv' must be

parse into vector<boost::string_view> using boost::spirit::x3

无人久伴 提交于 2019-12-23 12:36:14
问题 This is a follow-up question to my previous one regarding boost::spirit::x3 and boost::string_view . While I can parse into a std::vector<std::string> (live example), parsing into a std::vector<boost::string_view> fails with the following compile errors: #include <iostream> #include <string> #include <boost/utility/string_view.hpp> namespace boost { namespace spirit { namespace x3 { namespace traits { template <typename It> void move_to(It b, It e, boost::string_view& v) { v = boost::string

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

感情迁移 提交于 2019-12-22 01:24:39
问题 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

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

独自空忆成欢 提交于 2019-12-19 04:12:48
问题 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

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

谁说胖子不能爱 提交于 2019-12-17 19:27:10
问题 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

std::string_view and std::string in std::unordered_set [duplicate]

佐手、 提交于 2019-12-10 21:43:10
问题 This question already has an answer here : C++ unordered_map<string, …> lookup without constructing string (1 answer) Closed last year . Let's say you have an std::unordered_set<std::string> . You have an std::string_view object that you want to search for in the container. Problem is, you don't want to create a std::string from your std::string_view , as this kind of defeats the purpose of using std::string_view in the first place. However, it seems that std::string_view should be usable as

Why string_view instead of generalized container_view<T>?

怎甘沉沦 提交于 2019-12-09 02:27:53
问题 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