CppCon 2014: Herb Sutter "Back to the Basics! Essentials of Modern C++ Style"

  Рет қаралды 304,832

CppCon

CppCon

9 жыл бұрын

www.cppcon.org
--
Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2014
--
This talk revisits basic questions, such as how to declare and initialize a variable, how to pass a value to a function, how to write a simple loop, and how to use smart pointers, in the light of experience with C++11 and the latest C++14 refinements. This involves examining auto, rvalue references, range-for loops, uniform initialization, lambda expressions, unique_ptr and shared_ptr, and more.
--
Herb Sutter - Author, chair of the ISO C++ committee, software architect at Microsoft.
--
Videos Filmed & Edited by Bash Films: www.BashFilms.com
*-----*
Register Now For CppCon 2022: cppcon.org/registration/
*-----*

Пікірлер: 206
@dandymcgee
@dandymcgee 7 жыл бұрын
Whether or not you agree with what he says, nobody can disagree that this guy is a brilliant public speaker.
@darksidegirl
@darksidegirl 6 жыл бұрын
I don't agree. He's good in style, but his slides are TERRIBLE. He doesn't follow any recommendation on slide style I've read. He adds lots of text he doesn't read to the audience, who's trying to listen and read at the same time. He adds "taboo" colors, like light green, yellow and such. He uses different styles on each slide. Seriously, to be there in person, I'd never understand half of it, if you are not an expert.
@samaursa
@samaursa 5 жыл бұрын
@@darksidegirl good presentation style dictates you should not read what's on the slide directly. Slides supplement your talk. i would rather a talk by key note speakers be information dense even if I have to go back and watch it on KZbin again.
@alice_in_wonderland42
@alice_in_wonderland42 2 жыл бұрын
He's C++ guru He literally designs the new features along with other people in the Committee
@activelow9297
@activelow9297 Жыл бұрын
He's a brilliant bookist as well. I have several of his books on C++ and they are awesomesauce.
@arisweedler4703
@arisweedler4703 2 жыл бұрын
A passionate, humble, learned man giving an engaging talk. This feels like reading K&R felt. Such clarity and simplicity in that which seemed so complicated when learning it in other contexts.
@marciomaiajr
@marciomaiajr 2 жыл бұрын
K&R book is such a masterpiece. It's probably still the best book on the subject ever written. No other book comer close to its simplicity and compreensiveness.
@Xeverous
@Xeverous 4 жыл бұрын
Note: 47:30 (auto x = type{init}) making a temporary+move: this has been fixed in C++17 by copy elision. Type does not even have to be moveable.
@jvsnyc
@jvsnyc 3 жыл бұрын
I feel like the people who are still saying (even today) that cppcon is just an echo chamber of experts talking to each other, with little of value for an average developer might have had a point at some time in the past, but that certainly by the point of this talk it was not true. There have been countless great Back-To-The-Basics talks and others that show that they are very actively trying to increase the circle of C++ competency, and are not at all obsessed with hyper-expertise that could only be understood by a group of people that fits in an auditorium, much less an elevator. Great stuff.
@shavais33
@shavais33 Жыл бұрын
Brilliant work that still feels relevant 8 years later. re: auto type deduction - I think my fear of it comes from ending up with std header compilation errors later in the maintenance cycle. Working with C++ and watching videos like this one is making me smarter all the time, but I think I'm maybe just not quite smart enough yet. But this definitely convinces me to try using it more. re: C++ developers care a lot about performance - Well, the only time it really makes sense to use C++ is if performance is a higher priority than time to market.
@1aggin_5amurai
@1aggin_5amurai 3 жыл бұрын
Chandler and Herb are my favorites! Many thanks guys!
@wesofx8148
@wesofx8148 8 жыл бұрын
I agree with the philosophy here. The day I gave up on typing perfectly optimal code is the day I became 100x more productive
@ViolentFury1
@ViolentFury1 2 жыл бұрын
go write in php or java then.
@KFlorent13
@KFlorent13 2 жыл бұрын
@@ViolentFury1 Very nice answer.
@monsterhunter445
@monsterhunter445 2 жыл бұрын
@@ViolentFury1 you know java can be faster than c++. I will give you a hint it rhymes with git
@pabloortiz8007
@pabloortiz8007 Жыл бұрын
@@ViolentFury1 those don't let you "open the hood when you need to" as Herb described. Just.. Don't open the hood every time.
@hacker2ish
@hacker2ish 2 жыл бұрын
Herb is by far my favorite speaker from these conferences
@doBobro
@doBobro 4 ай бұрын
It's ironic how Herb come into realization C++ defaults are beyond repair and begun cppfront initiative in 2016.
@kamilziemian995
@kamilziemian995 Жыл бұрын
This is the hardest "Back to the Basics!" talk that I ever watch.
@MindControlMethod
@MindControlMethod 7 жыл бұрын
It drives me crazy that so many of this video shows Herb instead of his code!
@JacobMoen
@JacobMoen 6 жыл бұрын
Go and grab the slides, and have Herb and his code at the same time! :)
@0xCAFEF00D
@0xCAFEF00D 6 жыл бұрын
Yeah especially 56:00 and forward. He even draws attention specifically to the importance of watching the screen right then. Whoever put this together doesn't care. He's just mesmerized by Herb I guess.
@LukeGlue
@LukeGlue 5 жыл бұрын
MrSnowman Yeah seriously. It is like they swapped to a fresh camera guy that wanted to get more action.
@oxgubc
@oxgubc 5 жыл бұрын
slides available here github.com/CppCon/CppCon2014/blob/master/Presentations/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style%20-%20Herb%20Sutter%20-%20CppCon%202014.pdf
@siwenzhang
@siwenzhang 9 жыл бұрын
This is extremely useful, especially the parameter passing.
@rishiniranjan1746
@rishiniranjan1746 3 жыл бұрын
such a simple and honest guy
@Btw_visit_____todacarne-com
@Btw_visit_____todacarne-com 3 жыл бұрын
Great talk.Thanks. Watching it a little late but I think most of the advice still stands.
@pomlover
@pomlover 9 жыл бұрын
As usual, great stuff Herb!
@danielphd5072
@danielphd5072 5 жыл бұрын
Thank you very much Dr Herb Sutter
@ben2280
@ben2280 6 жыл бұрын
the content is great but the cut's are horrible, there are so many times the slides would be helpful but only the speaker is visable
@slava_xd
@slava_xd 5 ай бұрын
41:10 been thinking about this for a while. Nice to see it being covered here
@QuentinUK
@QuentinUK 8 жыл бұрын
"C++ is evolving man, we're going faster." 2
@hikkenwayans
@hikkenwayans 9 жыл бұрын
THANKS for the upload!!!
9 жыл бұрын
1:15 the concept of "unteachable" perfectly illustrated
@eXerigumoClanjor
@eXerigumoClanjor 9 жыл бұрын
And, I found that it's easier to line up all those equal signs (tabularize them) with auto
@MercedeX7
@MercedeX7 9 жыл бұрын
why is the camera mostly on him rather than the slides?
@vonkruel
@vonkruel 9 жыл бұрын
Quite worthwhile.
@nguonbonnit
@nguonbonnit Жыл бұрын
Wow ! So great. You help me a lots.
@1aggin_5amurai
@1aggin_5amurai 3 жыл бұрын
1:33:00 You can do it with structured binding in C++17: _std::set< int > digits = { 1, 2, 3, 5, 6 ,7, 8, 9, 0 }; // missed 4_ _for( int i = 0; i < 10; ++i )_ _{_ _auto const& [it, result] = digits.insert( i );_ _if( result )_ _{_ _std::cout
@kim15742
@kim15742 5 жыл бұрын
I rewound to the 42:00 "but wait there's more", I find that quite funny
@simeyabate4849
@simeyabate4849 2 жыл бұрын
It is 2022 and this presentation is still extraordinary and the speaker is fenomenal! By far the best c++ presentation I have ever attended!
@AlexeyArkhipenkoComposer
@AlexeyArkhipenkoComposer 4 жыл бұрын
There is a mistake on this slide: i.imgur.com/1nqH4Ct.png Actually, here you can see that mistake is in PDF presentation, but not on the screen behind Herb: kzbin.info/www/bejne/rp_UhX56bdp3bJY
@SoftNFuzzy
@SoftNFuzzy 9 жыл бұрын
If we're no longer encouraged to pass-by-value on setter functions, does this mean we should also discourage copy-and-swap assignment operators, which suffer from the same problems?
@abrorabyyu6221
@abrorabyyu6221 2 жыл бұрын
ah, i need this
@dainiusfigoras
@dainiusfigoras 8 жыл бұрын
35:09 "why deduce correctness" IMO weak argument to go to auto, because if you change something so significant that you starting modifying, when you haven't before, it's not small change, and have need to look at code more careful (because now types are different) is just a plus.
@prednaz4769
@prednaz4769 6 жыл бұрын
When it comes to option 4 for set_name, Herb, the confident guy in the audience and the guy expressing his surprise about its performance advantage in the end curiously all messed it up at some point. Herb firstly messed it up, because compilation of the code on his slide will fail as soon as used with a string argument because of the exclamation mark. Without it, compilation will still fail as soon as used with a char* argument, as during his benchmark. Herb is simply wrong with his "Write it right", instead do not constrain the template unless the function is overloaded. So the confident guy in the audience is right saying Herb constrained the template wrong. But it is even more wrong than he apparently expected. Because of the erroneous exclamation mark, the function will indeed accept a string literal. Finally the guy expressing his surprise about the performance advantage of option 4 appears to have a valid point at first sight because without the exclamation mark the template should generate at compile time the exact code of option two. But it only does so if used with a string argument. A char* argument causes compilation to fail as mentioned before. If the entire constraint had been correctly left out, then there would be no reason for surprise either because the generation of a char* template instantiation enabling the usage of string::operator=(const char*) clearly accounts for the performance advantage. Despite Herb's attempts to shift responsibility onto Howard, I think, Howard is the only one involved who has got everything right. He was dealing with an overloaded function in a different context like a constructor overloaded with copy and move constructors. His function being overloaded with a function taking some type X, in order to prevent the forwarding reference from matching X too, he had to write an template constraint to exclude X from the template, hence the exclamation mark. Herb just blindly copied Herb's code from a different context, fiddled with the data types according to his faulty understanding of the purpose of the constraint, and ended up with something which is wrong for multiple reasons.
@adaai2384
@adaai2384 5 жыл бұрын
You have, without intending to, demonstrated everything that is wrong with C++.
@skaniskin
@skaniskin 3 жыл бұрын
@@adaai2384 well, it was exactly the Herb's point - Howard is one of 5 people in the world who can write it right
@masquue1465
@masquue1465 2 жыл бұрын
Thanks for the clear explanation. Even Herb, who's lecturing this, make such near absurd mistake. It should serve as an alert.
@xenocampanoli815
@xenocampanoli815 4 жыл бұрын
At roughly 23:00 he is nearing his statement about dropping a reference count and what to do about it. 23:23 is perhaps the closest point to start, but before is better.
@GeorgeTsiros
@GeorgeTsiros 5 жыл бұрын
did the video have the audio gaps before?
@rukascool
@rukascool 5 жыл бұрын
'at least i thought he was a friend' let me get some popcorn, this is gonna be good
@ooltimu
@ooltimu 9 жыл бұрын
What does he mean "range-for doesn't support early break" ? I see it does.
@dunga309
@dunga309 3 жыл бұрын
for (e : c) does not work as of Visual Studio Preview 16.11.
@childhood1888
@childhood1888 2 жыл бұрын
I really need to watch this on a pc
@dribeas
@dribeas 9 жыл бұрын
No. The main difference is that the copy-and-swap idiom is not about avoiding unnecessary work, but about providing stricter guarantees, in particular regarding exception safety. If you want to have the strong exception safety guarantee, you must pay the price and create a new temporary object, for that you cannot leverage the memory that your current object holds.
@fabio-franco
@fabio-franco 9 жыл бұрын
About prefering auto for declaring local variables: The auto keyword makes code harder to read (type deduction) or document. It makes it worse even, because if you change a return type you don't want undesirable side effects that pass undetected. The compile time error is your friend when refactoring a code base. Much better than having run-time erros and it also gives the opportunity to review the code and detect problems ahead of time.
@LukeGlue
@LukeGlue 5 жыл бұрын
You're kind of missing a big piece of what he is saying. If you're coding to an interface then you can use his "tracking" auto declaration but if you are coding to an implementation then you need to use his "stick" or "commit" style which is 'auto x = type { foo() };'. This is better b/c it conveys that your intention is to code to a specific type and not an interface. It is more expressive.
@isodoublet
@isodoublet 5 жыл бұрын
"This is better b/c it conveys that your intention is to code to a specific type and not an interface. It is more expressive." No, it's literally just more typing.
@tissuepaper9962
@tissuepaper9962 Жыл бұрын
@@isodoublet you're still not understanding the distinction between "interface" and "implementation". You shouldn't need to know the exact type in most situations if you're relying on the interface instead of the implementation. If you were in the business of using or extending 3rd-party libraries you would understand why this distinction is important.
@____KB
@____KB 6 жыл бұрын
@ 30:05 Can someone explain to me what it means to make a type _track_ or _stick_? Beyond deducing the type or explicitly declaring the type I do not understand what these terms mean, nor have I heard them outside of this talk. Thank you.
@bingbang6543
@bingbang6543 5 жыл бұрын
It just means that auto "tracks" the type itself. For example if you have to classes class Tool; class Hammer : Tool; If you now have an object of the type "Hammer" but you want it for any reason to be sure it should be stored as a Tool and not a hammer, you should stick it.
@Chris-St-DE
@Chris-St-DE 7 жыл бұрын
Question for the slide "pass by value" (1:12:00) : Would the code work also without the std::move? Is it used only for performance?
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
Sure, it would work. However it would be slower for longer strings (short strings are actually copied and moved at the same speed). Long things short : without move -> allocation and memcpy ; with move -> copy of a pointer. When not using move, you are calling the copy assignment operator that 1. resize the buffer of the name_ string (allocation) 2. copy the content of the buffer of name into the buffer of name_ 3. delete the buffer of name at function return. When using move, you are calling the move assignment operator that 1. delete the current buffer of name_ 2. bind the buffer pointer of name_ to the buffer pointer of name 2. set the buffer pointer of name to nullptr (or equivalent)
@rajeevmehta7673
@rajeevmehta7673 6 жыл бұрын
At 1:17:00 setName function is marked as noexcept and takes an rvalue parameter and perfectly forwards it. But if setName is passed an lvalue, it might cause an allocation inside the function as it would be an assignment? In that case it cant be marked noexcept?
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
True. The expression inside the noexcept qualifier make the function noexcept if and only if the String type can be assigned to std::string without throwing. Otherwise it is not noexcpet qualified.
@FKATS-oq8xx
@FKATS-oq8xx 6 жыл бұрын
This is all very interesting, but it would also make sense IMHO to, at some point in the life of the C++ standardization process, spend some time to ensure that the C++ standard library is usable with Unicode text. I mean, usable as in Python 3, for instance.
@jonathan_cline
@jonathan_cline 4 жыл бұрын
didn't know languages that aren't Turing complete were fully usable
@TheMrKeksLp
@TheMrKeksLp 3 жыл бұрын
@@natewind Couldn't agree more! The only thing you actually care about is byte length of a string. Everything else is way too complex and specialized
@patrickqian8762
@patrickqian8762 9 жыл бұрын
a small advice for the video maker, you need to focus the camera a little more on the slide.... we need time to read that.... thank you
@jamesmcmanus
@jamesmcmanus 5 жыл бұрын
I remember folks making the same doom-and-gloom arguments against var when it was added to C#, but they were quickly proven wrong and now everyone uses it to great effect on readability and refactorability. C++ introduced auto only a few years later, and people are still debating it. Is the C++ community just reflexively contrarian?
@dustensobotta1698
@dustensobotta1698 9 жыл бұрын
The auto keyword is evaluated at compile-time.
@charshi08
@charshi08 8 жыл бұрын
A lot of food for thought.
@MM-24
@MM-24 7 жыл бұрын
Is there a cheat sheet for all this? (especially the unique_ptr and pass by ref vs pass by pointer etc)
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
yes: CPP Core Guidelines
@vvnurmi
@vvnurmi 9 жыл бұрын
Perhaps he means that range-for doesn't let you write a break condition that depends on the iterator. You only see the values behind the iterator.
@ruimvfigueira
@ruimvfigueira 9 жыл бұрын
Like Herb mentioned: "It reflects bias to code against implementations, not interfaces". Good code design should focus on "what" the code does, and not "how" it does it. This is particularly important if you are creating libraries for other people to use.
@levelont5560
@levelont5560 6 жыл бұрын
1:24:10 "...when you copy you also have to get rid of the previous state; when you move you don't" He mean't the exact oposite, right?
@gcanary562
@gcanary562 5 жыл бұрын
No, he is right
@gcanary562
@gcanary562 5 жыл бұрын
imagine a variable x and y. If you create variable x, it exists on memory. If you are copying x to y, x is still alive. But if you move it, you move the complete variable and x is no longer alive.
@kim15742
@kim15742 5 жыл бұрын
@@gcanary562 Only true if it is trivially destructible
@CarloBernaschina
@CarloBernaschina 5 жыл бұрын
​@@kim15742 can you explain why the type must be trivially destructible? shared_ptr is non trivially destructible and can be moved (it can just be moved). I think that the best example is vector. If you copy the original one remains untouched, but you need to allocate memory for the internal buffer of the second one and you need to execute the copy constructors of every Item (they may be trivially copied, but you need to copy them). If you move a vector it is left to the implementation of the move constructor to decide what to do; the move constructor can leave the original vector in an invalid state, what it must do is make the new one valid. A naive implementation of a vector can be a raw pointer to Foo and two size_t to store the current size of the vector and the current buffer size. In this case the move constructor can just copy the raw pointer and the two sizes in the new vector and set the raw pointer in the old one to nullptr, which makes it invalid. The non trivial distructor can just check for this state (is the raw pointer nullptr) and do nothing.
@brainplot
@brainplot 5 жыл бұрын
@@CarloBernaschina the destructor might as well avoid the check as deleting a nullptr is defined behavior and results in a no-op.
@wronglyNeo
@wronglyNeo 7 жыл бұрын
Can anyone explain me what the NOT-operator (!) is doing in the perfect forwarding example? From what I see the author wants to restrict the templatisation to the type std::string. I'm referring to this line: template::value>> I don't understand why the NOT operator is there. In my opinion it results exactly in the opposite of what shall be achieved.
@prednaz4769
@prednaz4769 6 жыл бұрын
Please refer to my other comment below this video.
@noxmetus
@noxmetus 5 жыл бұрын
The concept of clear simple code is not so clear. In one of my teams people didn't know range-for loop, it wasn't clear for them. “Clear” code for the developers in that team was the code that the least knowledgeable C++ developer could understand, practically C style. Otherwise they used to say it's difficult to follow. Also less function calls was more clear for them. A thousand lines of code in one function was better then 10 function calls, because they said otherwise they have to search and check each function code to follow. But what if the function name is very clear I used to argue? Doesn't matter. No amount of arguments you might hear from Herb, or Bob, or Kelvin, or Sean could help. They opposed even auto. Ah, and no templates. What I am saying is it might be quite challenging to serve that ~3M community of C++ developers. Sometimes I think we have to revert C++ back to C to make code “clear” for them. They program --C++.
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
The main problem is that they are reluctant to accept change like many "experienced" guys. But any C++ programmer who cannot understant a range-for loop is dumb or (more likely) in bad faith.
@noxmetus
@noxmetus 5 жыл бұрын
They can understand it. It's just not what they consider as clean code. What I am trying to say is that if we try to serve 3M community of C++ developers, we must admit that we have to forget about clean code in Herb's sense. Most of these 3M developers program to implementation, not to interface, and don't want to learn anything new.
@von_nobody
@von_nobody 9 жыл бұрын
Hi I'm Jon and I'm CA :D
@tricky778
@tricky778 8 жыл бұрын
Is there any room to change the spec for a simpler solution to the shared_ptr dereferencing problem at 23:48? Ideally, the library specification should have been that dereferencing operations on std::shared_ptr&lt;T&gt; should all return a std::shared_ptr_deref&lt;T&gt; object that ups the refcount during its lifetime and which has an implicit conversion to T&amp;. This way the reference will always be held while the object is being used. I hope that can be changed in upcoming c++ versions
@TanzinulIslam
@TanzinulIslam 7 жыл бұрын
you can just copy the shared_ptr to increment the refcount, no?
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
What about the overhead of such solution ? Ref counting is thread safe in case you forgot, which makes it very expensive in a context of repeated pointer dereferencing. Here, you have to take care of yourself in these quite rare cornercases. Otherwise, it's as fast as it can get.
@sradnz015
@sradnz015 8 жыл бұрын
"ptr"
@QuentinUK
@QuentinUK 8 жыл бұрын
Hi with the Herbs.
@xgihavoc
@xgihavoc 9 жыл бұрын
This is a good point. In most projects I've worked on, the first one is by far the most common (maybe because it's older) but very easily recognizable as "oh, that's an STL object iterator!". The other thing to look at here is how readable it is to new developers of C++. In my opinion, it makes more sense and is more readable because you are saying: "I'll start the beginning, until I reach the end, and advance one object per iteration" pretty explicitly.
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
Does it make python an obfuscated language ?
@Neme112
@Neme112 3 ай бұрын
You have it exactly backwards. You're just used to the old way of doing it, but for a novice it is the opposite.
@ihoryalovetskyi1310
@ihoryalovetskyi1310 5 жыл бұрын
So, why the fourth bar is so small in the last benchmark (the on the right)?
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
For the performance to be like that, you need your char* to be an rvalue in the first place. set_name("John"); //OK char* name = "John"; set_name(name) //Not OK Now, your temporary char* is passed to the templated set_name. The compiler then specializes the template with T = char*&& (rvalue reference). Then, the reference collapsing rules apply and make 'char* && &&' become 'char* &&'. However, the parameter name is an lvalue, just as any reference (rvalue or lvalue ref). Thus, it must be "converted back" to an rvalue before being sent to the assignment operator of string. This is what std::forward is made for. Finnaly, string::operator=(char*&&) is called. This causes the char* to be used by the string directly as its buffer (no copy, no assignment). In the end : 1 allocation to create the char*, no conversion, no copy. The other approaches involve at least one more allocation for the conversion from char* to string followed by deletions.
@skaniskin
@skaniskin 3 жыл бұрын
​@@gabrielb.2886 string::operator=(char*&&) there is no such an operator. The difference is because with const& + && you first call string(char*) with likely frequent allocation, then call a move constructor vs with option 4 you call string::operator=(char*) which with high probabilty incurs no memory allocation
@raf.nogueira
@raf.nogueira 8 жыл бұрын
I want so much to become a professional C++ programmer , i know simples things for now i do computer science . But where I live there is not any possibility of chance to get any job or oportunity...
@user-wv9pr3mz4f
@user-wv9pr3mz4f 7 жыл бұрын
aww poor you, go cry some more
@Conenion
@Conenion 7 жыл бұрын
To become a professional C++ programmer, you have to become a professional in algorithms and data structures first. Along with a lot of practical experience, not only in programming. Knowing a programming language well doesn't make you a professional.
@raf.nogueira
@raf.nogueira 7 жыл бұрын
Conenion i study graphs , binary trees , algorithm complexity , all kind of sorting algorithms and arrays structures ,pop push etc , every kind of things in the university I don't know everything but I can handle
@skaniskin
@skaniskin 3 жыл бұрын
The mindblowing thing is that by Sep 2014 they still didn't decide on the name for Type&&
@isitanos
@isitanos 3 жыл бұрын
With mandatory RVO in C++17, it should now always be ok to return by value no matter the size of the created object.
@clerothsun3933
@clerothsun3933 2 жыл бұрын
NRVO is not mandatory though.
@Neme112
@Neme112 3 ай бұрын
Except that in many cases RVO doesn't apply, and it's impossible for an average developer to reason about when it does apply and when it doesn't due to so many edge cases :/
@Neme112
@Neme112 3 ай бұрын
1:15:00 What's problematic regarding noexcept isn't the function itself. What's problematic is C++'s automatic implicit copying that can happen basically anywhere without the programmer noticing.
@BitPuffin
@BitPuffin 9 жыл бұрын
Why wouldn't you be able to use it? You just call "get" and get the raw pointer.
@edwardyang8254
@edwardyang8254 6 ай бұрын
IMO for (e : c) makes coding "easier" but not "simpler" than for (auto& e : c). Conceptually, both are the same thing, with the latter actually be clearer that e is a reference to an element in c. But implementation wise, the former requires less typing.
@johnappleseed8839
@johnappleseed8839 6 жыл бұрын
Did someone handpick that thumbnail clip? :P
@mahdies9620
@mahdies9620 3 жыл бұрын
I iove c++
@Banefane
@Banefane 8 жыл бұрын
Well, allright lets make c++ more powerful even though I don´t believe that every change will be successfully adapted :)
@AG-ld6rv
@AG-ld6rv 2 жыл бұрын
He didn't quite convince me that mass auto is the right way to go. In some cases, when a change in one place tracks to all the other variables, I'd be afraid of it silently resulting in the wrong behavior. Of course, auto is needed for quickly writing templates too (and his initial example was template-looking code!). I also like auto when it takes away huge types that provide no real information for sure such as vector::size_type. I'm also all right with it reducing redundant type information as he showed. However, I like types! They document what kind of operations might follow next as a type restricts what might be done using that variable. I wonder what Bjarne Stroustrup's modern C++ guide recommends. They're also essential for forwarding references and for writing the output of a function in terms of the arguments. The rest of the talk was great information. It can be hard to maintain interest for close to 2 hours about programming, but he pulled it off.
@TheBuilder
@TheBuilder 2 жыл бұрын
cool beans
@zachansen8293
@zachansen8293 29 күн бұрын
I used to really love Herb's talks until I realized the talk was usually the only thing to come out of them :( maybe a prototype on godbolt.
@minciNashu
@minciNashu 7 жыл бұрын
40:07 "try sticking it to the right, it will feel weird the first couple of times" lol
@Luftbubblan
@Luftbubblan 7 жыл бұрын
If i want to get my hands on gcc 7 where do i go?
@Luftbubblan
@Luftbubblan 7 жыл бұрын
Nvm, look like i'm gonna try Clang instead. Seems to be a somewhat related version in my repo.
@JiveDadson
@JiveDadson 6 жыл бұрын
Correction, Herbie. The type of "Hello" is not const char*. It's char[6]; 40:56
@Neme112
@Neme112 3 ай бұрын
I just tried it and it is const char*.
@10100rsn
@10100rsn 9 жыл бұрын
"This is the slide ..." @ 58m25s
@1aggin_5amurai
@1aggin_5amurai 3 жыл бұрын
47:12 _auto m = std::mutex();_ _auto lg = std::lock_guard( m );_ is perfectly legal thanx to copy/move elision in C++17
@frankwang6707
@frankwang6707 8 жыл бұрын
no subtitile?
@childhood1888
@childhood1888 2 жыл бұрын
36:00
@lockbert99
@lockbert99 8 жыл бұрын
Why is he saying "share putter" and not "shared pointer" ?
@TopconBob
@TopconBob 7 жыл бұрын
He's just pronouncing ptr as "putter." Like he says "stud" for std.
@mydeenyussouf7517
@mydeenyussouf7517 5 жыл бұрын
But why?
@mikevasiljevs412
@mikevasiljevs412 7 жыл бұрын
This is quite a weird beast "template", I guess I am visiting too few TMP hackathons!
@mikevasiljevs412
@mikevasiljevs412 7 жыл бұрын
can somebody please explain why this is a good thing?
@PixelThorn
@PixelThorn 4 жыл бұрын
@@mikevasiljevs412 PM me and we can discuss it - I am not sure ai will convince you though
@ggrrffasmr3018
@ggrrffasmr3018 Жыл бұрын
20:00
@soulstudiosmusic
@soulstudiosmusic 9 жыл бұрын
9:30 - answer is, the top one is easier to parse visually by someone coming to the code who hasn't written it. In fact it would be even clearer without the 'auto' as the type would be immediately visible.
@tissuepaper9962
@tissuepaper9962 Жыл бұрын
You've got Stockholm syndrome from C if you think the standard for loop is "easier to parse visually" than a range-based for. To even be able to understand the top code, you already have to know about iterators and whatnot, and once you know that stuff the second code is much clearer because it provides the exact same information in a condensed form, without all this extra visual noise. There is literally nobody on the planet who would be able to understand the top without being able understand the bottom, so why not just use the bottom code? Why should a developer have to waste their time writing out boilerplate when they could just write out exactly what's relevant and nothing more?
@Knirin
@Knirin 4 ай бұрын
Yeah, auto really does hide useful information.
@ChristianParpartDev
@ChristianParpartDev 5 жыл бұрын
This is all nice and clean; but in that case, I'd like to get that `auto` be renamed to `let` or `var` for variable declarations. and something similar to function declaration (instead of `auto f(int x) -> int;` something like `func f(int x) -> int;` but then again, the next issue is parameter declaration lists, which are still right-to-left. :)
@c2com
@c2com 9 жыл бұрын
c2com interface the code 2015
@xgihavoc
@xgihavoc 9 жыл бұрын
Even during this talk, I feel Herb himself is a victim of what he is trying to teach against. The simple mention of "does this every time you are iterating through all elements" instead of "any time you iterate" means that you have to have two variations depending on whether you iterate through some or all. Isn't this "non-default" in itself?
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
It is default. More than 70% of my for loops are range-for loops. Other loops are either reverse iterations or index based iterations because I need the index value.
@tissuepaper9962
@tissuepaper9962 Жыл бұрын
How often do you actually write a for-loop that doesn't interact with the whole collection in order? It's rare, most of the counterexamples are just doing the whole collection in reverse order, or index-based for-loops when the author needed the index for some reason. Don't think of range-based for as a rule, it's just a design pattern. It's a simple way to make the old iterator-based syntax more brief and clear.
@Gargantupimp
@Gargantupimp 5 жыл бұрын
Hmm nothing but critics in the comment section, but 1k likes/30 dislikes... Do CPP programmers just like to complain?
@robotpotato6892
@robotpotato6892 5 жыл бұрын
yes
@manawa3832
@manawa3832 5 жыл бұрын
That Mike Acton guy needs to watch this
@Bloodthirst
@Bloodthirst 4 жыл бұрын
Not really , mike works on AAA titles where he actually has to live within the 13ms limit which is THE time for people to over optimize and use every micro optimization to run things faster , so its justified in his case
@JiveDadson
@JiveDadson 6 жыл бұрын
There must be hundreds of boilerplate writers (obfuscation engineers) at Dinkumware.
@HebaruSan
@HebaruSan 7 жыл бұрын
Auto... what a debugging nightmare it would be if the types of objects were hidden like that.
@longlostwraith5106
@longlostwraith5106 6 жыл бұрын
Thankfully the types are visible to debugging tools. What people fear the most is having a type you didn't intend to have, IMO in that case do use explicit type or the auto syntax he showed.
@TheEVEInspiration
@TheEVEInspiration 7 жыл бұрын
I had to shake my head a few times so far. Using smart-pointers is always messy, it promotes not thinking about the lifetime of objects at an architectural level. It creates multiple ways to do call chains that are fixed, while requirements might change at a deeper level. Better is to avoid the heap concept as much as possible, even if under the hood a heap is used, the stack model is the natural model for C++.
@Ashamaali
@Ashamaali 7 жыл бұрын
" it promotes not thinking about the lifetime of objects at an architectural level" Exactly. They undoubtedly have their uses, but this point needs to be made.
@grisevg
@grisevg 7 жыл бұрын
Watch his Cpp2016 talk "Leak-Freedom in C++" where he shows how to write simple, fast and leak-free code. And yes, he says that most of the time you should use stack and not heap.
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
@@Ashamaalidon't agree - using unique_ptr you always immediately know the lifetime
@gerhardbiebl9778
@gerhardbiebl9778 6 жыл бұрын
at 9:02 Herb Sutter encourages to prefer range loops over conventional counting loops. I cannot see any advantage in this whatsoever, if you don't count the additional array border safety. And i don't ! No developer in his right mind will produce an array border violation after 1 year of experience. Try this to judge for yourself : #include using namespace std; #using using namespace System; const unsigned int Size = 60000; void main() { char Numbers[Size]; DateTime EndTime = DateTime::Now; DateTime StartTime = DateTime::Now; TimeSpan elapsed; // Counting Loop: for (unsigned int i = 0; i < Size; i++) // forward iteration //for (unsigned int i = Size-1; i > 0; i--) // backward iteration { Numbers[i] = i; } EndTime = DateTime::Now; elapsed = EndTime - StartTime; cout
@mrlithium69
@mrlithium69 6 жыл бұрын
Apparently they are more concerned with Array Boundary Violation Safety than you.
@gerhardbiebl9778
@gerhardbiebl9778 6 жыл бұрын
Thank you - must have happened when i inserted that line along with other alternative comments. Very astute ! Well done! But what really went wrong there was that accidentially (maybe because i was working on a project where i used this one) i chose the "wrong" implementation of the range loop - in fact, this code partially is "Managed C++" instead of C++11, which means i used a for each loop from .NET including the "System.dll". The proper loop header schould be: "for( char& ri : Numbers )" which is c++11 and not "for each (char& ri in Numbers)" which is managed c++. In MFC there also is a "for_each" range loop (Note the underscore). But that one would need iterators, which a primitive type char array does not have. This is the cause why they introduced an "Array" class in C++11 which exposes iterators even for primitive types.
@seditt5146
@seditt5146 6 жыл бұрын
You do see the irony in bragging about why not to do something when at the same time making a mistake showing exactly why to use something...
@Elite7555
@Elite7555 6 жыл бұрын
Look, the "conventional" for-loop with this manual counter is just cumbersome. Most of us just wish to loop through an array in a comfortable way, like in Java or JavaScript. For your special cases, you can do your traditional for-loop, but it is not the rule.
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
Can you please do the same with a structure without random access iterators such as a list ? Can you also give me one good reason to continue using c style arrays on the heap when std::vector has almost zero runtime overhead while providing everything you need for both forward and backward iteration ? And finally, why should I bother with a counter if everything I do is "for each element, do something, regardless of its actual index" ? The only reasons I can think of to use a counter is when you actually need the index and when you are iterating over several containers simultaneously.
@kkiimm009
@kkiimm009 7 жыл бұрын
auto x = 10; Why not remove it all together and just write x = 10;
@marcchapman6560
@marcchapman6560 7 жыл бұрын
Because that becomes an assignment statement not an initialiser statement
@varphonewong4068
@varphonewong4068 5 жыл бұрын
If without auto, how to resolve the parameter and local variable? void foo(int x) { auto x = 10; } // ok void foo(int x) { x = 10; } // ok void foo(int& x) { auto x = 10; } // ok void foo(int& x) { x = 10; } // wtf?
@logangraham2956
@logangraham2956 Жыл бұрын
​@@varphonewong4068 your recreating the variable x. void foo(int x) { auto x = 10; } // your creating x as an int in the parameter and then your creating it again as an int ("auto") in the body, does this even compile?
@logangraham2956
@logangraham2956 Жыл бұрын
​@@varphonewong4068 nope! , error: declaration of ‘auto x’ shadows a parameter
@svenvandevelde1
@svenvandevelde1 9 ай бұрын
Who came up with the idea of unique and shared pointer language construct. It is ugly, confusing. Why was C++ simply not extended with new keywords?
@fo3nixz877
@fo3nixz877 5 жыл бұрын
call me old fashioned, i still prefer procedural style
@dylanw4668
@dylanw4668 8 жыл бұрын
mistitle
@MeMelster
@MeMelster 7 жыл бұрын
isn't there also a risk that auto will fuck up my shit? For example: double vec[2]={2, 5.3}; auto dummy=0; dummy=vec[1]; Now fummy will be just "5" because i missed to type "0.0" if i just had used double it would be no problem.
@levelont5560
@levelont5560 6 жыл бұрын
That has nothing to do with "auto" and everything with you doing an implicit type conversion at the "dummy=vec[1];" assignment.
@qqi239z123
@qqi239z123 9 жыл бұрын
Addicted to complexity indeed: cannot have a talk without self-contradictions. Non-owning raw pointer means private destructor, private destructor, means make_unique, make_shared cannot be used. But a really nice try anyway.
@gabrielb.2886
@gabrielb.2886 5 жыл бұрын
He was mostly talking about function parameters. When you just use an object that belong to someone else, may it be on the stach, on the heap, a smart_ptr or anything. I can't see where you need a private destructor to do that.
@lockbert99
@lockbert99 8 жыл бұрын
I envy people who code in languages that don't have things like "r-value references" and have experts arguing about whether something should be called a "universal reference" or a "forward reference".
@brainplot
@brainplot 4 жыл бұрын
The complexity of the language comes with the great power it gives. Those people you talk about and envy probably don't even know what a pointer is and what it's used for. If you're watching this video I suppose you enjoy the benefits C++ gives to developers. Well, that is the other face of the coin...
@Neme112
@Neme112 3 ай бұрын
@lot Rust or Go are pretty powerful as well and they're nowhere near as complex as C++. C++ just sucks as a language. It's obviously designed by a committee and not by someone with a coherent vision for the language. Every member of the committee gets in every feature they want. And it's a total mess overall without any good design.
Can C++ be 10x Simpler & Safer?  - Herb Sutter - CppCon 2022
1:54:00
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 13 МЛН
Omega Boy Past 3 #funny #viral #comedy
00:22
CRAZY GREAPA
Рет қаралды 36 МЛН
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 14 МЛН
When someone reclines their seat ✈️
00:21
Adam W
Рет қаралды 22 МЛН
code::dive conference 2014 - Scott Meyers: Cpu Caches and Why You Care
1:16:58
NOKIA Technology Center Wrocław
Рет қаралды 183 М.
CppCon 2015: Herb Sutter "Writing Good C++14... By Default"
1:29:06
CppCon 2016: Jason Turner “Practical Performance Practices"
1:00:29
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 13 МЛН