Earlier quoted context omitted.
There are also some things that are powerful but I'm not quite sure if I really like them. For instance, he promotes the use of handles instead of pointers. He goes through these variants: Gadget* p = new Gadget(n); // not exception safe shared_ptr p{new Gadget(n)}; //exception safe unique_ptr p{new Gadget(n)}; //exception safe and less wasteful than shared_ptr if local Gadget g{n}; //his preferred solution My proble…
C++11 move semantics eliminates the need of deep copying in most cases and return by value usually is very cheap.
Bjarne Stroustrup – The Essence of C++ [video]
41–50 of 85 posts
Re: Bjarne Stroustrup – The Essence of C++ [video]
#42As somebody who attended this conference, and hadn't had much exposure to C++11 features outside of 'auto', my personal biggest takeaway from almost every talk was this: Stop passing your sink variables as const refs. That is to say if you have a constructor: MyClass::MyClass(const std::string& s) : m_s(s) {} That you call like: std::string s = "Some string"; MyClass c(s); You're hamstringing the compiler into always…
Re: Bjarne Stroustrup – The Essence of C++ [video]
#43I look at Java code and it horrifies me. Java is great if your resource is memory. It turns into a horrible unsafe mess the moment your resources are not memory. //C++ RAII goodness. void foo() { conn.open(); file.open(); printer.activate(); //do stuff } //Java's lack of RAII is disturbing void foo() { try{ conn.open(); file.open(); printer.activate(); //do stuff } catch(Exception e) { } finally{ try { conn.close();…
Re: Bjarne Stroustrup – The Essence of C++ [video]
#44Earlier quoted context omitted.
try/finally in Java / using in C# are designed for that scenario.
And try/finally assigns the cleanup responsibility to the caller, not the callee, which just adds boilerplate and mental burden. C++ does not need a finally block due to RAII. The using block (and "try-with-resource" in Java 7) is a poor man's RAII emulation. Anyway, what if you need to share non-memory resources? Suddenly you cannot depend on the garbage collector, you cannot use try/finally, you cannot use using or…
Re: Bjarne Stroustrup – The Essence of C++ [video]
#45As somebody who attended this conference, and hadn't had much exposure to C++11 features outside of 'auto', my personal biggest takeaway from almost every talk was this: Stop passing your sink variables as const refs. That is to say if you have a constructor: MyClass::MyClass(const std::string& s) : m_s(s) {} That you call like: std::string s = "Some string"; MyClass c(s); You're hamstringing the compiler into always…
Is the std::move call really necessary? It's a little sad if the compiler can't see that s is already an rvalue.
Re: Bjarne Stroustrup – The Essence of C++ [video]
#46I look at Java code and it horrifies me. Java is great if your resource is memory. It turns into a horrible unsafe mess the moment your resources are not memory. //C++ RAII goodness. void foo() { conn.open(); file.open(); printer.activate(); //do stuff } //Java's lack of RAII is disturbing void foo() { try{ conn.open(); file.open(); printer.activate(); //do stuff } catch(Exception e) { } finally{ try { conn.close();…
You can write bad code in any language. No experienced Java coder would write a code like this. Java has ARM blocks for dealing with deterministic resource cleanup.
Re: Bjarne Stroustrup – The Essence of C++ [video]
#47Earlier quoted context omitted.
Is the std::move call really necessary? It's a little sad if the compiler can't see that s is already an rvalue.
s isn't an rvalue. It has a name, and you can refer to it within the body of the constructor.
move calls strike me as ugly and kinda dangerous. What if you do wind up wanting to refer to s in the body later on? It'd be nice if the compiler just handled it.
Re: Bjarne Stroustrup – The Essence of C++ [video]
#48Earlier quoted context omitted.
You can write bad code in any language. No experienced Java coder would write a code like this. Java has ARM blocks for dealing with deterministic resource cleanup.
using/ARM/etc. make classes which own non-memory resources a lot less awkward to deal with, but one of the big wins of RAII is that the caller doesn't have to know or care that you have things which require special cleanup.
On the other side, some programming techniques are hard or downright impossible without GC - e.g. heavy functional programming with immutable persistent structures.
Re: Bjarne Stroustrup – The Essence of C++ [video]
#49As somebody who attended this conference, and hadn't had much exposure to C++11 features outside of 'auto', my personal biggest takeaway from almost every talk was this: Stop passing your sink variables as const refs. That is to say if you have a constructor: MyClass::MyClass(const std::string& s) : m_s(s) {} That you call like: std::string s = "Some string"; MyClass c(s); You're hamstringing the compiler into always…
Is the std::move call really necessary? It's a little sad if the compiler can't see that s is already an rvalue.
Re: Bjarne Stroustrup – The Essence of C++ [video]
#50really interesting to see classes as ressource manager above all, instead of concept incarnation. I was almost convinced to go back to C++ until the slide with auto range and all, combining new features of C++, at which point i remembered why i didn't want ro have a look at C++ code again. but hey, i'm not a system programmer so i can afford it :)
RAII ("classes as resource manager") is one of the nice concepts in C++. My favourite is certainly const correctness. And then there's tons of things that creep me out a bit. Being a multi paradigm language sounds good on paper, but it seems to cause a lot of accidental complexity. The best way to stay sane is probably to pick a certain subset of C++ for your project and stick to that, that's what I tend to do. Then…