5 ms·
> Someone at Yandex recently did a presentation about Rust[1] in which they pointed to a bit of (completely idiomatic!) C++11 code that caused undefined behavio
by bjz_ 12y ago
> Someone at Yandex recently did a presentation about Rust[1] in which they pointed to a bit of (completely idiomatic!) C++11 code that caused undefined behavior.
It would be great to have at least that segment of the talk translated. Sounds like a good example.
- pbsd 12y agoSee slide 42 of STL's recent talk for what I guess will be a similar example: https://github.com/CppCon/CppCon2014/tree/master/Presentations/STL%20Features%20And%20Implementation%20Techniques%20-%20Stephan%20T.%20Lavavej%20-%20CppCon%202014 https://github.com/CppCon/CppCon2014/tree/master/Presentatio...
- dbaupp 12y agoReproduced here: const regex r(R"(meow(\d+)\.txt)"); smatch m; if (regex_match(dir_iter->path().filename().string(), m, r)) { DoSomethingWith(m[1]); } - What's wrong with this code? - Haqrsvarq orunivbe va P++11 - Pbzcvyre reebe va P++14 - .fgevat() ergheaf n grzcbenel fgq::fgevat - z[1] pbagnvaf vgrengbef gb n qrfgeblrq grzcbenel (http://rot13.com/ http://rot13.com/ 'd if you want to guess.)
- nikbackm 12y agoSeems like this was fixed in C++14 by adding a std::string&& overload. http://en.cppreference.com/w/cpp/regex/regex_match http://en.cppreference.com/w/cpp/regex/regex_match
- dbaupp 12y agoThe underlying problem is still there, fixing a few of the worst cases in the standard library is helpful but only up to a point. (E.g. anyone with a custom function that does something in a similar vein needs to remember to do the same.)
- tiles 12y agoThe slides on Slideshare are surprisingly easy to follow (and were a good introduction for me): http://www.slideshare.net/yandex/rust-c http://www.slideshare.net/yandex/rust-c
- dbaupp 12y agoThe example: std::string get_url() { return "http://yandex.ru"; } string_view get_scheme_from_url(string_view url) { unsigned colon = url.find(':'); return url.substr(0, colon); } int main() { auto scheme = get_scheme_from_url(get_url()); std::cout << scheme << "n"; return 0; }
- ufo 12y agoCan you say what is the problem here? What is a string_view?
- dbaupp 12y agoA non-owning pointer into string memory owned by someone else (effectively a reference into some string). AIUI, the problem is the temporary string returned by get_url() is deallocated immediately after the get_scheme_from_url call, meaning that the string_view reference `scheme` is left dangling.
- shin_lao 12y agoThe string_view pattern is a pretty bad idea and useless with a decent compiler.
- dbaupp 12y agoWhat do you mean by useless? If I have a string like "foo bar baz" and I want the second word, should I copy out that data into a whole new string? That seems rather inefficient. (How is a compiler going to optimise that away?)
- shin_lao 12y agoFor small strings, a copy is not only faster but more multithreading friendly. Keep in mind that on a 64-bit architecture a view is at least 16 bytes large and that small strings can be copied to the stack resulting in better locality and reduced memory usage. Last but not least, with copy elision, your temporaries might not even exist in the first place. Example: std::string data; // ... auto str = data.substr(2, 3); // pretty sure str will be optimized away if (str[0] == 'a')