Тёмный

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

CppCon
Подписаться 153 тыс.
Просмотров 307 тыс.
50% 1

www.cppcon.org
--
Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/Cpp...
--
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/reg...
*-----*

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 200   
@Xeverous
@Xeverous 5 лет назад
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.
@MindControlMethod
@MindControlMethod 7 лет назад
It drives me crazy that so many of this video shows Herb instead of his code!
@JacobMoen
@JacobMoen 7 лет назад
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 6 лет назад
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
@arisweedler4703
@arisweedler4703 3 года назад
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.
@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
@dandymcgee
@dandymcgee 8 лет назад
Whether or not you agree with what he says, nobody can disagree that this guy is a brilliant public speaker.
@darksidegirl
@darksidegirl 7 лет назад
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 6 лет назад
@@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 RU-vid again.
@alice_in_wonderland42
@alice_in_wonderland42 3 года назад
He's C++ guru He literally designs the new features along with other people in the Committee
@activelow9297
@activelow9297 2 года назад
He's a brilliant bookist as well. I have several of his books on C++ and they are awesomesauce.
@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 2 года назад
@@ViolentFury1 those don't let you "open the hood when you need to" as Herb described. Just.. Don't open the hood every time.
@shavais33
@shavais33 2 года назад
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.
@doBobro
@doBobro 8 месяцев назад
It's ironic how Herb come into realization C++ defaults are beyond repair and begun cppfront initiative in 2016.
@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.
@siwenzhang
@siwenzhang 9 лет назад
This is extremely useful, especially the parameter passing.
@dainiusfigoras
@dainiusfigoras 9 лет назад
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.
@MercedeX7
@MercedeX7 9 лет назад
why is the camera mostly on him rather than the slides?
@noxmetus
@noxmetus 6 лет назад
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 6 лет назад
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 6 лет назад
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.
@1aggin_5amurai
@1aggin_5amurai 3 года назад
Chandler and Herb are my favorites! Many thanks guys!
@prednaz4769
@prednaz4769 7 лет назад
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.
@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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xnqTKD8uD64.html
@Gargantupimp
@Gargantupimp 6 лет назад
Hmm nothing but critics in the comment section, but 1k likes/30 dislikes... Do CPP programmers just like to complain?
@robotpotato6892
@robotpotato6892 5 лет назад
yes
@ooltimu
@ooltimu 10 лет назад
What does he mean "range-for doesn't support early break" ? I see it does.
@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
@QuentinUK
@QuentinUK 8 лет назад
"C++ is evolving man, we're going faster." 2
@eXerigumoClanjor
@eXerigumoClanjor 10 лет назад
And, I found that it's easier to line up all those equal signs (tabularize them) with auto
@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
@sradnz015
@sradnz015 9 лет назад
"ptr"
@dunga309
@dunga309 3 года назад
for (e : c) does not work as of Visual Studio Preview 16.11.
@hacker2ish
@hacker2ish 2 года назад
Herb is by far my favorite speaker from these conferences
@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
@rishiniranjan1746
@rishiniranjan1746 3 года назад
such a simple and honest guy
@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
@Neme112
@Neme112 7 месяцев назад
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.
@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 8 лет назад
aww poor you, go cry some more
@Conenion
@Conenion 8 лет назад
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 8 лет назад
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
@SoftNFuzzy
@SoftNFuzzy 10 лет назад
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?
@GeorgeTsiros
@GeorgeTsiros 5 лет назад
did the video have the audio gaps before?
@edwardyang8254
@edwardyang8254 10 месяцев назад
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.
@svenvandevelde1
@svenvandevelde1 Год назад
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?
@levelont5560
@levelont5560 7 лет назад
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 6 лет назад
No, he is right
@gcanary562
@gcanary562 6 лет назад
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.
@zachansen8293
@zachansen8293 4 месяца назад
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.
@dribeas
@dribeas 10 лет назад
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.
@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?
@pomlover
@pomlover 10 лет назад
As usual, great stuff Herb!
@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 7 лет назад
Please refer to my other comment below this video.
@____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 6 лет назад
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.
@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.
@TheBuilder
@TheBuilder 2 года назад
cool beans
@fabio-franco
@fabio-franco 10 лет назад
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 6 лет назад
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.
@Astfresser
@Astfresser 3 года назад
I also don't buy Herb's arguments for auto. I never liked it because it covers up types and makes code much less expressive. Everyone wants his code to be "self documenting" but with auto you take exactly this away. So when I try to read code from another code base I would be faster to interpret what it is doing even if the initialization spans a whole line and doesn't look pretty... I think behind the scenes most of the bias towards auto anyway stems from the fact that "it looks nicer" and you can line up your equal signs better - which really shouldn't be a valid argument for a programming language or should it? That's rediculous.
@tissuepaper9962
@tissuepaper9962 Год назад
Dude, have you ever tried initializing a nested container in C++? Nobody wants to type six right angle brackets and the same type name three times just to instantiate a 3d array, and they especially don't want to type out some of these 3rd-party library type names that span a whole line. Most of the time, the unabridged type name is just noise that makes it more difficult to see the logic. It's important for code to be clear about *intent.* It is not important for code to be clear about the exact implementation of every type you're using. Imagine if you couldn't use the "int" type name anymore, and had to specify the exact width of every int you used. It would get tiresome quickly, and in the vast majority of situations the exact width of the int isn't relevant. Only in a few situations does it make sense to say, specifically, "this is a uint8_t" or "this is an int16_t", almost always when you want to do some kind of fixed-point arithmetic or when you want to use an overflowing int for some purpose. It's sort of the same idea with "auto". Most of the time you don't need to see every single template parameter in detail, you just need to know broadly what type of object you're dealing with. In the situations where every minor detail of the object is relevant to the logic of the code, don't use auto. We shouldn't get rid of auto entirely just because it's better not to use it in certain situations. I propose that if you want to see all the details, you can still use "auto". Write a function that takes the relevant template parameters as its arguments, and then use "auto" when instantiating the object to keep the code uncluttered. That way you get code that looks like this: ```auto arr = ndarray(k = 3, T = float);``` Instead of like this: ```std::vector arr = std::vector();``` The first code is obviously better for clarity, and if you need to see the exact details that you can see in the second code, you can always go read the ndarray() function. There's no reason why your code should be cluttered up by all of the text that doesn't convey anything. You only need to know that it's a 3d array of floats, so why should you have to write all this extra noise? The code is there for the benefit of you and your fellow man. Source code isn't a set of instructions for the computer, it's a tool for communicating with other humans. The computer only understands assembly. If you really think that having a "pretty" language isn't important then I would challenge you to write literally any major program in assembly. I have a feeling you will never take me up on the challenge. Having code that looks nice and is easy to read is much more important than having optimized code in the majority of situations. Processor time is free these days, there's no reason to waste brain time doing what the computer can do at compile time. The developers' time is far and away the most expensive resource that goes into writing software.
@Knirin
@Knirin 8 месяцев назад
If you a whole line just for type information auto is just hiding the problem.
@skaniskin
@skaniskin 3 года назад
The mindblowing thing is that by Sep 2014 they still didn't decide on the name for Type&&
@isitanos
@isitanos 4 года назад
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 7 месяцев назад
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 :/
@ank2000
@ank2000 3 года назад
the irony of a c++ programmer advocating "simplicity" is palpable...
10 лет назад
1:15 the concept of "unteachable" perfectly illustrated
@mahdies9620
@mahdies9620 3 года назад
I iove c++
@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.
@slava_xd
@slava_xd 9 месяцев назад
41:10 been thinking about this for a while. Nice to see it being covered here
@svenvandevelde1
@svenvandevelde1 Год назад
C++ has become a mess ...
@Banefane
@Banefane 8 лет назад
Well, allright lets make c++ more powerful even though I don´t believe that every change will be successfully adapted :)
@nguonbonnit
@nguonbonnit Год назад
Wow ! So great. You help me a lots.
@xenocampanoli815
@xenocampanoli815 5 лет назад
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.
@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.
@kamilziemian995
@kamilziemian995 Год назад
This is the hardest "Back to the Basics!" talk that I ever watch.
@von_nobody
@von_nobody 9 лет назад
Hi I'm Jon and I'm CA :D
@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 6 лет назад
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 7 лет назад
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 6 лет назад
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.
@BitPuffin
@BitPuffin 9 лет назад
Why wouldn't you be able to use it? You just call "get" and get the raw pointer.
@johnappleseed8839
@johnappleseed8839 6 лет назад
Did someone handpick that thumbnail clip? :P
@childhood1888
@childhood1888 2 года назад
I really need to watch this on a pc
@rukascool
@rukascool 5 лет назад
'at least i thought he was a friend' let me get some popcorn, this is gonna be good
@danielphd5072
@danielphd5072 5 лет назад
Thank you very much Dr Herb Sutter
@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
yes: CPP Core Guidelines
@fo3nixz877
@fo3nixz877 5 лет назад
call me old fashioned, i still prefer procedural style
@ggrrffasmr3018
@ggrrffasmr3018 Год назад
20:00
@childhood1888
@childhood1888 2 года назад
36:00
@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
@lockbert99
@lockbert99 9 лет назад
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?
@xgihavoc
@xgihavoc 10 лет назад
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 6 лет назад
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.
@dustensobotta1698
@dustensobotta1698 10 лет назад
The auto keyword is evaluated at compile-time.
@abrorabyyu6221
@abrorabyyu6221 3 года назад
ah, i need this
@tricky778
@tricky778 9 лет назад
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 8 лет назад
you can just copy the shared_ptr to increment the refcount, no?
@gabrielb.2886
@gabrielb.2886 6 лет назад
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.
@ihoryalovetskyi1310
@ihoryalovetskyi1310 6 лет назад
So, why the fourth bar is so small in the last benchmark (the on the right)?
@gabrielb.2886
@gabrielb.2886 6 лет назад
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
@minciNashu
@minciNashu 7 лет назад
40:07 "try sticking it to the right, it will feel weird the first couple of times" lol
@soulstudiosmusic
@soulstudiosmusic 10 лет назад
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 8 месяцев назад
Yeah, auto really does hide useful information.
@c2com
@c2com 10 лет назад
c2com interface the code 2015
@10100rsn
@10100rsn 9 лет назад
"This is the slide ..." @ 58m25s
@HebaruSan
@HebaruSan 7 лет назад
Auto... what a debugging nightmare it would be if the types of objects were hidden like that.
@longlostwraith5106
@longlostwraith5106 7 лет назад
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.
@vonkruel
@vonkruel 9 лет назад
Quite worthwhile.
@charshi08
@charshi08 9 лет назад
A lot of food for thought.
@hikkenwayans
@hikkenwayans 9 лет назад
THANKS for the upload!!!
@QuentinUK
@QuentinUK 9 лет назад
Hi with the Herbs.
@dylanw4668
@dylanw4668 9 лет назад
mistitle
@frankwang6707
@frankwang6707 8 лет назад
no subtitile?
@xgihavoc
@xgihavoc 10 лет назад
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 6 лет назад
Does it make python an obfuscated language ?
@Neme112
@Neme112 7 месяцев назад
You have it exactly backwards. You're just used to the old way of doing it, but for a novice it is the opposite.
@JiveDadson
@JiveDadson 6 лет назад
There must be hundreds of boilerplate writers (obfuscation engineers) at Dinkumware.
@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!
@TheEVEInspiration
@TheEVEInspiration 8 лет назад
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 8 лет назад
" 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
@@Ashamaalidon't agree - using unique_ptr you always immediately know the lifetime
@ChristianParpartDev
@ChristianParpartDev 6 лет назад
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. :)
@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 2 года назад
​@@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 2 года назад
​@@varphonewong4068 nope! , error: declaration of ‘auto x’ shadows a parameter
@JiveDadson
@JiveDadson 6 лет назад
Correction, Herbie. The type of "Hello" is not const char*. It's char[6]; 40:56
@Neme112
@Neme112 7 месяцев назад
I just tried it and it is const char*.
@Btw_visit_____todacarne-com
@Btw_visit_____todacarne-com 4 года назад
Great talk.Thanks. Watching it a little late but I think most of the advice still stands.
@lockbert99
@lockbert99 9 лет назад
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 7 месяцев назад
@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.
@manawa3832
@manawa3832 6 лет назад
That Mike Acton guy needs to watch this
@Bloodthirst
@Bloodthirst 5 лет назад
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
@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.
Далее
Microservices are Technical Debt
31:59
Просмотров 362 тыс.