You know that something is amazing when it seems so natural and obvious that in hindsight you wonder why something like it wasn't done a decade earlier!
Thanks CppCon for share this videos early on! Look the number of views in few hours! Please continue posting other talks for all of us that unfortunately can't assist this great event with the C++ family. CppCon you're rock!!!
[Completing previous comment] As Herb said : "You take a copy of a shared pointer before you turn it into a raw pointer and pass it on a call tree". His point is that the tools he is presenting helps you to remember to do that.
2:00, I know this is just a silly example. But it's nice to point some things. I use to separate the midia (big data) of a circle, from "its logic", which is likely to fit on caches. So it becomes an "OO inspired by DOD" approach. As for for range loop, despite being a C++11 thing, I only passed to use it from C++14. It was required to specify the type. So on those years I used a macro of my own, which deduced the type, at the cost of using pointer syntax. I think "auto all the things" is gold. 3:31, good, because there are old things with bad reputation, that actually are pretty useful - macro, old enum, someone could say unions. So, don't make mistakes Carbon folks are already doing!
+Vasiliy Galkin I think it's because gsp is a global variable so f() or any other fuction f() may call can access gsp directly and modify it (f.e. call gsp.reset()), so gsp' could be invalid. Using sp means you increase the ref count so even if f() or any function f() calls modifies gsp, the ref count won't go down to 0 and delete the pointer's object. It confused me too because Herb wasn't clear about the fact that gsp can be accessed and modified from the functions, even tho he said that (was thinking about what can change the pointer and not the owner).
The one thing about this newer style of C++ that I really hate is the keyword "auto". I always find it harder to figure out what's going on without the types being laid out explicitly.
I don't really know C++. How can I learn how to do it how he mentions it @ 2:00 without having to go through the code on the left hand side.? In other words, learn C++14 from C [or scratch], not from C++03. I come from a C background as implied. I'd like to develop good habits from day one!
I am not an expert (at all), but I think the second method works because you create a copy of the smart point global_sp into the local sp. This should increment the number of existing references (pointers) to *global_sp (that's what smart pointers are for). With such a copy, it is now impossible for f() to completely invalidate global_sp using only sp.get() because there will be at least 1 more reference to *global_sp due to the copy before the call. RUclips response comments are too short >
reinterpret_cast is often wrongly used to convert "pointer to T" to "pointer to U" when T and U are "unrelated types" (e.g. char and unsigned char) and static_cast does not seem enough, but in fact static_cast really is enough: const U *pointer_to_const_U = static_cast (static_cast (pointer_to_const_T)); I believe reinterpret_cast remains useful for converting pointers to integrals (e.g. of type std::uintptr_t or std::size_t) and... anything else, anyone ? (-:
-1 because: Visual Studio is free tool if you are open source developer. Look at Community edition. Herb Sutter works for Microsoft so, yeah, he is firstly implementing everything for Visual Studio I guess and than it comes to clang or gcc. And at last but not least as Zarviroff Serge said - it doesn't matter - it's about C++ and you not IDE or operating system.
I wonder if these static analysis tools work when the memory validations happen separated in the code. For example if a function created a pointer to that vector... Oh forget it, since I started writing this he started to cover this point. :D
How does copying a shared_ptr protected from side effects from callees when both the orignal and copy shared ptr's point to the same underlying memory? [1:01:00] Dead confused.... Any replies?
***** Ok i remember the code. So what's the fix that Herb suggests for this dangling pointer problem? Surely any new shared ptrs put in will also point to the same ptr?
why isn't thing.bytes() thing.byteCount() or thing.numberOfBytes()? If you say in the explanation "number of bytes" but it still says bytes() which I would have presumed returned a byte array then you've done something wrong.
We all know very well what he meant -- a reference to const --, so we can mentally substitute. This is 90-minute talk with a thousand details. Some typos sneak in. But the meaning is clear.
I didn't get the example about shared_ptr (slide precisely at 1:00:00). What Herb claims: void f(int* p) { /* might invalidate p */ }; shared_ptr global_sp = ...; int main() { f(global_sp); // bad, and tool confirms it auto sp = global_sp; f(sp); // OK, checker tool also has no complaints. return 0; } I can't get it why second case is a good scenario. When we assign global_sp to sp, they both still point to the same data. Given that, f(sp) will invalidate both.
No, f(sp) could potentially invalidate global pointer, no it's data. If we change global_sp to new pointer value, any local copy of it is unaffected. So local sp pointing to original global_sp location, no problem at all.
It was about trying to convert a temporary value (also called: r-value. A variable without a name.) to a reference (also called: l-value reference). C++ doesn't support that. Why doesn't C++ support it? Because the temporary (r-value) is by default const. eg func("Hello world");
Exactly, both will be invalidated, because they both point to the same thing. Reference counter is not going to prevent that. I wonder how nobody complained about that. What you need in this case is another copy: auto sp = std::make_shared<int>( *gsp );
The community (incl Chandler Carruth, Kate Gregory and even Sutter) came to realize that Modern C++ and in particular move semantics were like lipstick on a pig. It is unusually complex for beginners, it requires much more CS theory knowledge (xvalue/prvalue etc) that should be handled by the compiler like in more contemporary languages as Rust and Carbon. It feels out of place. Many heavyweights are jumping boat, creating new languages due to the realization that Modern C++ hit a dead end.
I tried to use smart pointers in a smaller new project. I'm sorry but i can't get myself behind it. It uglifies the code where readability is the most important part. Lambdas and r"sql( )sql" strings are the best. I only want a better stdlib and multiple return values from functions. Unfortunately modern C++ is also using much more memory and performance. So i upgraded from C with classes to C with lambdas.
Karl Hatteland But struct splitting to put the values into variables or pass two result values into another function as 2 seperate arguments is not possible without built in compiler support. Yes it is all Syntax Candy and there is a way around it, i know, but then i could also use C and Brainfuck.
Have you thought that… well, maybe code is a bit cleaner if you use auto instead of std::vector::const_iterator? Writing short and readable code isn't a sign of laziness, it's a sign of being a good developer.
auto is compile time not runtime. It's not about laziness, it allows much easier refactoring amongst other things. How many libraries I've used where people do something like (int i =0; i < xx.size(); i++) { be_lazy(); } where size() returns a size_t, the compiler then throws a signed / unsigned mismatch warning, can easily be avoided with auto, it's still obvious what you are doing, and if the xx type gets swapped for something where size() returns a signed type, using auto means still no warning. It also prevents copies/conversions of objects where the declared type is mismatched to the assigned value type.
***** because of C++'s history of backwards compatibility, it occupies a niche filled but few other languages. C++ is already widely adapted; rather than being replaced, it is being evolved (whether you like it or not). Most new features aim to replace older, worse features. These features are retained as to not break the large amount of software built on C++; C++ does not have the luxury of quickly deprecating features. Talks like these aim to show how to fully utilize the newer features so that in the future the worse features may be "phased out"
Anyone else feel like they are trying to make a "C++ for dummies" language out of it? I mean, if you are writing in C++ you should have a full understanding of your pointer arithmetic and memory allocation/deletion. C++ doesn't need to be more simple, its already a great OOP language to use and very fast :)
Not feeling like that at all, to be honest. Also, I'm rarely inclined to people dummies for their mistakes, even though I'd be in a pretty good position to do that. After all, nobody can click on my RUclips-nick to look up a list of the last 20 bugs I fixed in my private code before committing it to some repository, to see if he, in hindsight, could call *me* a dummy. Also, I think I have different opinions on what constitutes "smarties". Devising or adapting an algorithm to solve an actual problem is an exercise that actually requires intelligence. Typing those ideas into some text editor is the least "smart" part of the whole activity. Finding a formal error in memory allocation/deletion doesn't require intelligence and the talk proofs that because they were able to create tools to do that. Wasting the focus and capacity of an intelligent mind on that would be a real dumb thing to do. C++ should not be a language that requires smart people who want to get actual hard problems solved (like mathematical problems) to do dumb exercises like reading to thousands of lines to look for memory allocation errors. Because that would make C++ a bad tool. I am happy to see that C++ is becoming a better tool through the great work of people like Sutter and Stroustrup, a tool that allows smart people to get work done effectively, without having to waste their time by gaining hours of experience in questions like "How do I map this list of several hundreds of compiler errors pointing to header-files, I've not even written, to find the single slip I made when using some template?". The tools should help them to use their time more efficiently.
Caydn Lofton exactly what I was thinking. lots of hand holding going on here. not trying to be a spoil sport, it just looks to me to be some bunch of standardising and simplification that might make harder to explain or requiring loads of commenting. as long as they don't actively remove older C++ standards and pointer arithmetic in the future, it's cool but once they start to over it all, that's when it might get ugly, especially for game programmers.
Some Coder Its the age old question: freedom or safety? Freedom to build and design in whatever way is best, even if it takes longer to make sure the code is safe, or safety knowing that a language is there to save you even though it also limits you from exploring
Caydn Lofton if the limitations are kept at a minimum with the language allowing for both safe and risky options, then we can all have cake. Give people the options for both and low level interaction. I think that's what most people want anyway
You know that something is amazing when it seems so natural and obvious that in hindsight you wonder why something like it wasn't done a decade earlier!
Thanks CppCon for share this videos early on! Look the number of views in few hours! Please continue posting other talks for all of us that unfortunately can't assist this great event with the C++ family. CppCon you're rock!!!
I could watch these all day
Herb explains things so much better than Bjarne. Everything is nice and clear, even for a beginner like me. Thanks Herb! :-)
Benjamin Rood I couldn't say, since I am yet to listen to many of the C++ guys, but he sure is a great one! :-)
Fantastic presentation!
Amazing work, getting memory safety without too much annotation is fantastic.
[Completing previous comment] As Herb said : "You take a copy of a shared pointer before you turn it into a raw pointer and pass it on a call tree". His point is that the tools he is presenting helps you to remember to do that.
2:00, I know this is just a silly example. But it's nice to point some things. I use to separate the midia (big data) of a circle, from "its logic", which is likely to fit on caches. So it becomes an "OO inspired by DOD" approach.
As for for range loop, despite being a C++11 thing, I only passed to use it from C++14. It was required to specify the type. So on those years I used a macro of my own, which deduced the type, at the cost of using pointer syntax. I think "auto all the things" is gold.
3:31, good, because there are old things with bad reputation, that actually are pretty useful - macro, old enum, someone could say unions. So, don't make mistakes Carbon folks are already doing!
+Vasiliy Galkin I think it's because gsp is a global variable so f() or any other fuction f() may call can access gsp directly and modify it (f.e. call gsp.reset()), so gsp' could be invalid.
Using sp means you increase the ref count so even if f() or any function f() calls modifies gsp, the ref count won't go down to 0 and delete the pointer's object.
It confused me too because Herb wasn't clear about the fact that gsp can be accessed and modified from the functions, even tho he said that (was thinking about what can change the pointer and not the owner).
Does a crossword-puzzle or scrabble board use a 2-dimensional string?
I love this video so much.Really appreciated.
Awesome presentation, very educational.. Thanks for sharing...
It kinda disturbs me that Herb pronounces things like "shared_ptr" as "shared putter" rather than "shared pointer"
+Vile Lasagna Same x_x
+Vile Lasagna I think he said "pointer" once.
In another video he also talked about stuhduh instead of std
He does that in this one too ^ ^
He wanted to rhyme sutter with putter
This is great! I can't wait for C++14! :D
The one thing about this newer style of C++ that I really hate is the keyword "auto". I always find it harder to figure out what's going on without the types being laid out explicitly.
If you use a modern IDE, it always tell you what's the type is just by having the mouse on the variable name.
I don't really know C++. How can I learn how to do it how he mentions it @ 2:00 without having to go through the code on the left hand side.? In other words, learn C++14 from C [or scratch], not from C++03. I come from a C background as implied. I'd like to develop good habits from day one!
I am not an expert (at all), but I think the second method works because you create a copy of the smart point global_sp into the local sp. This should increment the number of existing references (pointers) to *global_sp (that's what smart pointers are for). With such a copy, it is now impossible for f() to completely invalidate global_sp using only sp.get() because there will be at least 1 more reference to *global_sp due to the copy before the call. RUclips response comments are too short >
reinterpret_cast is OK if you're interfacing with a C-style library (like OpenGL for example)? I use them there.
reinterpret_cast is often wrongly used to convert "pointer to T" to "pointer to U"
when T and U are "unrelated types" (e.g. char and unsigned char)
and static_cast does not seem enough,
but in fact static_cast really is enough:
const U *pointer_to_const_U = static_cast (static_cast (pointer_to_const_T));
I believe reinterpret_cast remains useful for converting pointers to integrals (e.g. of type std::uintptr_t or std::size_t) and... anything else, anyone ? (-:
-1 because:
Visual Studio is free tool if you are open source developer. Look at Community edition.
Herb Sutter works for Microsoft so, yeah, he is firstly implementing everything for Visual Studio I guess and than it comes to clang or gcc.
And at last but not least as Zarviroff Serge said - it doesn't matter - it's about C++ and you not IDE or operating system.
does someone know how to reproduce the errors for example 1 using code analysis @46:30 , what rule to set ?
Good question, of someone discover please tell us
Lots of points for correctly using the term "dangling pointer". Too many otherwise good sources seem to confuse the meaning.
I've written it wrong, f(global_sp) should be replaced by f(global_sp.get()), and f(sp) --> f(sp.get()).
Yet, my question remains open.
I wonder if these static analysis tools work when the memory validations happen separated in the code. For example if a function created a pointer to that vector... Oh forget it, since I started writing this he started to cover this point. :D
How does copying a shared_ptr protected from side effects from callees when both the orignal and copy shared ptr's point to the same underlying memory? [1:01:00]
Dead confused....
Any replies?
+MaidePlays So its upping the count by 1 to keep things alive.
Is this a threading issue?
+MaidePlays I dont remember the code.
***** Ok i remember the code. So what's the fix that Herb suggests for this dangling pointer problem? Surely any new shared ptrs put in will also point to the same ptr?
***** A copy of the shared Ptr I remember.
Ok - I tried your sample code and as you say it works. Thanks!
why isn't thing.bytes() thing.byteCount() or thing.numberOfBytes()? If you say in the explanation "number of bytes" but it still says bytes() which I would have presumed returned a byte array then you've done something wrong.
You are a university student I presume?
The examples around 1:20:55 don't compile because you can't bind a non-const int& to the return of std::min
We all know very well what he meant -- a reference to const --, so we can mentally substitute. This is 90-minute talk with a thousand details. Some typos sneak in. But the meaning is clear.
So is there a similar tool for linux ?
Yes, certainly. In Linux, one can use Virtual Machine to run Windows with the wonderful Windows applications such as Visual C++. He. He, he, he.
I didn't get the example about shared_ptr (slide precisely at 1:00:00).
What Herb claims:
void f(int* p) { /* might invalidate p */ };
shared_ptr global_sp = ...;
int main() {
f(global_sp); // bad, and tool confirms it
auto sp = global_sp;
f(sp); // OK, checker tool also has no complaints.
return 0;
}
I can't get it why second case is a good scenario. When we assign global_sp to sp, they both still point to the same data. Given that, f(sp) will invalidate both.
No, f(sp) could potentially invalidate global pointer, no it's data. If we change global_sp to new pointer value, any local copy of it is unaffected. So local sp pointing to original global_sp location, no problem at all.
I don't get it, one guy on panel exclaims std::max is wrong and now they saying std::min is wrong.
It was about trying to convert a temporary value (also called: r-value. A variable without a name.) to a reference (also called: l-value reference). C++ doesn't support that.
Why doesn't C++ support it? Because the temporary (r-value) is by default const. eg func("Hello world");
Why only VS? Why don't tall about free IDE and what about user of Linux ?)
Exactly, both will be invalidated, because they both point to the same thing. Reference counter is not going to prevent that. I wonder how nobody complained about that. What you need in this case is another copy: auto sp = std::make_shared<int>( *gsp );
Why old programmers were so smart :
1. Working with unsafe language
2. No Social Media
3. No CS all of them were mathematician.
So …
Woohoo! Variants!
1:09:17 he said eminem, half-life 3 confirmed
The community (incl Chandler Carruth, Kate Gregory and even Sutter) came to realize that Modern C++ and in particular move semantics were like lipstick on a pig. It is unusually complex for beginners, it requires much more CS theory knowledge (xvalue/prvalue etc) that should be handled by the compiler like in more contemporary languages as Rust and Carbon. It feels out of place. Many heavyweights are jumping boat, creating new languages due to the realization that Modern C++ hit a dead end.
There are languages if you don't want to bother these type of stuff, like python etc. I think these stuff make C++ what C++ is.
Good job
But C++14 is already out...
I tried to use smart pointers in a smaller new project. I'm sorry but i can't get myself behind it. It uglifies the code where readability is the most important part. Lambdas and r"sql( )sql" strings are the best. I only want a better stdlib and multiple return values from functions.
Unfortunately modern C++ is also using much more memory and performance. So i upgraded from C with classes to C with lambdas.
+Lothar Scholz multiple return values? Return a struct with multiple members?
Karl Hatteland But struct splitting to put the values into variables or pass two result values into another function as 2 seperate arguments is not possible without built in compiler support.
Yes it is all Syntax Candy and there is a way around it, i know, but then i could also use C and Brainfuck.
return a tuple, and use std::tie to assign to variables in the calling scope
en.cppreference.com/w/cpp/language/structured_binding
Good fun
Complaining about heavy annotation in a language that still relies on header files...
32:48
better name: a better default profile for type safety in c++14
No, you don't create a copy, you just create another object that points to the same thing (same memory address). A real copy would do the job.
Every time I see an " *auto* " part of my soul dies a little.
(-_-) why? auto is so useful... oh u are talking about auto keyword in C++98.. yeah, that indeed eats part of one's soul.
it's like "var": I don't really know what's going on here so I'll just leave it for the runtime/type checker to figure out. Also it promotes laziness.
Wait what ? auto has nothing to do with runtime ...
Have you thought that… well, maybe code is a bit cleaner if you use auto instead of std::vector::const_iterator? Writing short and readable code isn't a sign of laziness, it's a sign of being a good developer.
auto is compile time not runtime. It's not about laziness, it allows much easier refactoring amongst other things. How many libraries I've used where people do something like (int i =0; i < xx.size(); i++) { be_lazy(); } where size() returns a size_t, the compiler then throws a signed / unsigned mismatch warning, can easily be avoided with auto, it's still obvious what you are doing, and if the xx type gets swapped for something where size() returns a signed type, using auto means still no warning. It also prevents copies/conversions of objects where the declared type is mismatched to the assigned value type.
Why are they making it so difficult to use? It's insane, good thing I switched to Java...
+Paul Orekhov It's safe and VERY fast (much unlike Java...)
+Milo Turner that is complete bullshit.
***** I would, I just don't understand why they have to make C++ so complex, the only thing I like about it is the STL, that's it.
+Mariano c++ is hardly just object oriented. when they add variants it will be just as functional your average fpl
***** because of C++'s history of backwards compatibility, it occupies a niche filled but few other languages. C++ is already widely adapted; rather than being replaced, it is being evolved (whether you like it or not). Most new features aim to replace older, worse features. These features are retained as to not break the large amount of software built on C++; C++ does not have the luxury of quickly deprecating features. Talks like these aim to show how to fully utilize the newer features so that in the future the worse features may be "phased out"
Anyone else feel like they are trying to make a "C++ for dummies" language out of it? I mean, if you are writing in C++ you should have a full understanding of your pointer arithmetic and memory allocation/deletion. C++ doesn't need to be more simple, its already a great OOP language to use and very fast :)
Not feeling like that at all, to be honest. Also, I'm rarely inclined to people dummies for their mistakes, even though I'd be in a pretty good position to do that. After all, nobody can click on my RUclips-nick to look up a list of the last 20 bugs I fixed in my private code before committing it to some repository, to see if he, in hindsight, could call *me* a dummy. Also, I think I have different opinions on what constitutes "smarties". Devising or adapting an algorithm to solve an actual problem is an exercise that actually requires intelligence. Typing those ideas into some text editor is the least "smart" part of the whole activity. Finding a formal error in memory allocation/deletion doesn't require intelligence and the talk proofs that because they were able to create tools to do that. Wasting the focus and capacity of an intelligent mind on that would be a real dumb thing to do.
C++ should not be a language that requires smart people who want to get actual hard problems solved (like mathematical problems) to do dumb exercises like reading to thousands of lines to look for memory allocation errors. Because that would make C++ a bad tool. I am happy to see that C++ is becoming a better tool through the great work of people like Sutter and Stroustrup, a tool that allows smart people to get work done effectively, without having to waste their time by gaining hours of experience in questions like "How do I map this list of several hundreds of compiler errors pointing to header-files, I've not even written, to find the single slip I made when using some template?". The tools should help them to use their time more efficiently.
Caydn Lofton exactly what I was thinking. lots of hand holding going on here. not trying to be a spoil sport, it just looks to me to be some bunch of standardising and simplification that might make harder to explain or requiring loads of commenting. as long as they don't actively remove older C++ standards and pointer arithmetic in the future, it's cool but once they start to over it all, that's when it might get ugly, especially for game programmers.
Some Coder Its the age old question: freedom or safety? Freedom to build and design in whatever way is best, even if it takes longer to make sure the code is safe, or safety knowing that a language is there to save you even though it also limits you from exploring
Caydn Lofton if the limitations are kept at a minimum with the language allowing for both safe and risky options, then we can all have cake. Give people the options for both and low level interaction. I think that's what most people want anyway
Some Coder Have you heard of C#? :p haha C++ 11 is already very high level programming. But thats just my opinion
55:55