Live data from Hacker News

Bjarne Stroustrup – The Essence of C++ [video]

channel9.msdn.com

41–50 of 85 posts

Re: Bjarne Stroustrup – The Essence of C++ [video]

#41
post #35

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.

But move semantics only work with temporaries. They don't magically make every object a handle.

Re: Bjarne Stroustrup – The Essence of C++ [video]

#42
post #31

As 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]

#43

I 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]

#44
post #36

Earlier 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…

True, but 90% of resources is memory. Java makes 90% of resources much easier to handle and 10% only little harder (like always use the try-catch-finally or try-with-resources).

Re: Bjarne Stroustrup – The Essence of C++ [video]

#45
post #31

As 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.

s isn't an rvalue. It has a name, and you can refer to it within the body of the constructor.

Re: Bjarne Stroustrup – The Essence of C++ [video]

#46

I 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.

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.

Re: Bjarne Stroustrup – The Essence of C++ [video]

#47

Earlier 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.

I understand that, but it's clearly not used in the body. I was hoping it would be a legal and implemented optimization.

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]

#48

Earlier 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.

Agreed, however it is very easy to catch a forgotten close() automatically (IDEs do that), and, at least in my experience, most non-memory resources are used locally and not shared, so they are pretty easy.

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]

#49
post #31

As 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.

Once things have a name and you can take their address they are no longer an rvalue. It would require a much more sophisticated type system to do this automatically (when you pass a reference to another function, does it capture it?). It would also be non-obvious when you were moving. Think of Java style escape analysis vs C++ stack allocation (implicit vs explicit). There is one case where the compiler will do this though, when you return a stack allocated value from a function, it is implicitly moved.

Re: Bjarne Stroustrup – The Essence of C++ [video]

#50
post #7
post #5

really 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…

C++ const-correctness is weaker than immutability in, say Haskell or Scala.
Post reply on HN