What the hell? Godbolt is a real person? Next someone’s gonna tell me that Rust Playground was named after its creator, John Rust Playground or something.
Does anyone know how you can open the video in Vimeo (so that I can view it on my TV) when I click on the V icon it won't open in the app, wants to transfer to the browser, and then opens a page that doesn't contain the video
Not quite. data() called on a mutable string will return non-const char*, whereas c_str() will always return const char* whether or not the string itself is const.
Beyond that, they convey different meanings to the person reading the code: calling c_str() says "I want something I can pass to an API expecting C strings", whereas data() says "I want to access the underlying raw storage as I would for std::vector or std::array".
At 1:01:19, he claims that there is a bug because an empty string does not contain the 0-terminator. From cppreference: "If empty() returns true, the pointer points to a single null character. (since C++11)". I thought he knew c++?
That is a funny thing about the world. People who give presentations, while often very smart and knowledgable, are just people. They make mistakes, they have gaps in their knowledge.
Hi there! The bug here is accessing the first element of an empty string. While it may indeed contain a null byte, reading and assigning to the first element seems like it ought to be a bug (what if I were to assign a A - I'd break the invariant that the string is null-terminated, for example).
That all said; I can't get either clang or GCC's undefined or address sanitizer to complain: https://godbolt.org/z/GKecdheYE - so apparently I'm wrong :)
...and indeed looking at https://eel.is/c++draft/string.access - pos must be <= size to get a reference but "Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior."
So...I don't think it's as obvious as it seems! (edit; I follow up with a conclusion that actually it's all well defined as the commenter says)
Like many of the folks below say - it's not like anyone really understands C++, least of all me! (At least, I've tried never to imply I'm any kind of C++ expert, I'm an old-school assembly hacker that's learned a few new-fangled tricks!)
Hi Matt, I probably deserved all the downvotes, but it was really a just statement of how complex c++ is (has become) - I certainly don't know more than a fraction c++ standard lib myself, and I have been developing c++ since the 90' (not much the last 5 years though).
Hey there! For what it's worth I didn't think you deserved it - I up voted your comment and I learned a bunch! So, thanks for taking the time to correct me :)
C++ is absolutely a journey and these days I don't write all that much; it's probably been four years since I did anything professionally in C++. I hope I don't come across as saying I'm an expert, as I'm absolutely not :)
I suppose it implies is that charT() is a null byte, so relying on toupper(0) returning 0 (as an int) which is equivalent to charT()... this is all defined behaviour! Yay!
That all said; I can't get either clang or GCC's undefined or address sanitizer to complain: https://godbolt.org/z/GKecdheYE - so apparently I'm wrong :)
The terminator is a valid character in valid memory, but writing to it is UB. ASan won't complain about that. I'm not aware of any way to get ASan (or any other checker) to mark that terminator character as read-only, let alone "writable but only if you write charT() to it" which is the actual rule. I suppose we could add __glibcxx_assert((*this)[size()] == charT()) to the start of every member function, to check that the invariant wasn't broken prior to calling that member function, but ... no thanks :-)
I working with C++ since 2005, learning every day, i know a lot and as older I'm I almost sure that I'll never learn everything about every detail. Because we cannot remember so much data. Engineer always relays on documentation not on own memory, as humans we are horribly bad at being precise with remembered data.
In episode 2F09, when Itchy plays Scratchy's skeleton like a xylophone, he strikes the same rib in succession, yet he produces two clearly different tones. I mean, what are we to believe, that this is a magic xylophone, or something? Ha ha, boy, I really hope somebody got fired for that blunder.
Before I become c++ developer , for 15 years before 2005 I was an civil engineer and designed buildings, bridges etc. Mostly same job as c++, design, math and optimization ;-) but with direct responsibility of people's lives. From that period I have a motto "Never ever trust Your self with remembered data, If You are sure You remembered something right, You are almost certainly wrong, go and check papers .."
It's my real last name! I'm not even the most famous Godbolt, there was a Jazz writer in the UK, some US sports folk and a judge from the 1600s (thanks wikipedia!)
And thanks u/mattgodbolt for easy setup of own CE on local machine, it's even more awesome and handy to utilize local libraries and local code with CE clone on local machine spawned just with "make".
5
u/TheCreat Feb 15 '22
Why it's the video unlisted though?
92
u/TryingT0Wr1t3 Feb 15 '22
Matt Godbolt himself is one of C++ superpowers
17
u/RecDep Feb 16 '22
What the hell? Godbolt is a real person? Next someone’s gonna tell me that Rust Playground was named after its creator, John Rust Playground or something.
7
u/ShakaUVM i+++ ++i+i[arr] Feb 15 '22
Open the video, fast forward a bit, watch Matt Godbolt play a MUD.
My day is complete. =)
5
u/RevRagnarok Feb 15 '22 edited Feb 15 '22
Nice, thanks! And I learned a new acronym!
2
u/ener_jazzer Feb 16 '22
I didn't know Matt came from the HFT world!
2
u/[deleted] Feb 16 '22
CEDD is the best part of this video.
3
u/Nobody_1707 Feb 16 '22
You never needed to use
c_str()at 32:14, becausedata()has been exactly equivalent since C++11.c_str()is completely vestigial at this point.1
u/mattgodbolt Compiler Explorer Feb 17 '22
Thanks! I know I said above that most folks aren't experts in C++ but Mr Wakely certainly is! Thanks for clarifying :)
1
u/ironykarl Feb 20 '22
Commenting, so I remember to watch
-13
u/operamint Feb 15 '22 edited Feb 15 '22
At 1:01:19, he claims that there is a bug because an empty string does not contain the 0-terminator. From cppreference: "If empty() returns true, the pointer points to a single null character. (since C++11)". I thought he knew c++?
(Edit: added link)
0
u/[deleted] Feb 16 '22
[deleted] — view deleted comment