Why Make Your Classes Final? -- Andrzej Krzemieński
Why make your classes final?
I was often wondering why anyone would want to prohibit inheritance from one’s class in C++. Recently I came across one good example where this is useful...
March 23-28, London, UK
By Blog Staff | Sep 30, 2012 01:33 PM | Tags: intermediate basics
Why make your classes final?
I was often wondering why anyone would want to prohibit inheritance from one’s class in C++. Recently I came across one good example where this is useful...
By Blog Staff | Sep 26, 2012 03:53 AM | Tags: experimental advanced
Unifying Generic Functions and Function Objects
I just got finished collaborating on a proposal with Faisal Vali and Herb Sutter to include generic lambdas and pythy functions in the core language. After the upcoming Portland committee meeting, we should have a good sense of how much appetite there is on the committee for including these features in C++.
While we were writing that paper, we got some of our most helpful comments and insights from Mathias Gaunard. It was a pivotal moment when Mathias reminded us that we could create operator overload sets explicitly with inheritance and using declarations, and then used it to demonstrate "overloaded lambda expressions..."
By Blog Staff | Sep 20, 2012 02:17 PM | Tags: intermediate
Hot of the press at Dr. Dobb's:
Padding and Rearranging Structure Members
By Dan Saks
... Multibyte objects often have an alignment ... a "requirement that objects of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address". The Standard leaves it up to each target processor to specify its alignment requirements. That is, a processor might require that a 4-byte integer or pointer referenced as a single object be word aligned — at an address that's a multiple of four. A processor also might require that an 8-byte floating-point number be word aligned, or maybe even double-word aligned — at an address that's a multiple of eight.
According to the C Standard, a program that attempts to access an improperly aligned object produces undefined behavior. This means that the program is in error, but the exact consequences of that error are platform-dependent. With many processors, an instruction that attempts to access improperly aligned data issues a trap. With other processors, an instruction that accesses misaligned data executes properly but uses up more cycles to fetch the data than if the data were properly aligned...
By Blog Staff | Sep 17, 2012 09:01 AM | Tags: advanced
Via C++ Next, hat tip to Dave Abrahams:
Chris Kohlhoff’s ASIO library contains an extraordinary little header, not in the public interface, but in the examples directory, that implements what he calls “Stackless Coroutines” (very similar to Python’s Simple Generators if you’re familiar with those). He does it completely portably, with just a few macros, and considering that there are zero lines of platform-specific code, they work amazingly well.
Dave cites this article that describes how to use the coroutines:
A potted guide to stackless coroutines
Keen-eyed Asio users may have noticed that Boost 1.42 includes a new example, HTTP Server 4, that shows how to use stackless coroutines in conjunction with asynchronous operations. This follows on from the coroutines I explored in the previous three posts, but with a few improvements. In particular:
- the pesky entry pseudo-keyword is gone; and
- a new fork pseudo-keyword has been added.
The result bears a passing resemblance to C#'s yield and friends. This post aims to document my stackless coroutine API, but before launching into a long and wordy explanation, here's a little picture to liven things up...
By Blog Staff | Sep 10, 2012 12:37 PM | Tags: intermediate
Stackoverflow [c++] question of the day:
Resources for understanding C++ performance?
One interesting followup:
The Performance TR is freely available via the WG21 site. Quick search, top hits including PDF, HTML, and some announcements at the time: bing.com/search?q=C%2B%2B+performance+TR .
By Blog Staff | Aug 31, 2012 05:44 PM | Tags: advanced
Just posted on Sutter's Mill, "Reader Q&A: How to write a CAS loop using std::atomics." From the post:
A colleague asked:
How should one write the following “conditional interlocked” function in the new C++ atomic<> style?
[...]
The short answer is:
T old = a;
while( old >= 0 && !a.compare_exchange_weak( old, old+val ) )
{ }
For the longer answer, continue reading...
By Blog Staff | Aug 20, 2012 09:20 AM | Tags: advanced
New article by Andrzej Krzemieński:
User-defined literals — Part I
This post is about the new language feature: the ability to specify user-defined literals. You may already be familiar with it, for example from N2750. In this post we will talk about literals in general, the purpose and usefulness of user-defined literals, their limitations, and alternatives in C++ to achieve similar goals.
By Blog Staff | Aug 17, 2012 09:01 AM | Tags: intermediate advanced
Interesting new C++Next article about destructors that throw:
Evil, or Just Misunderstood?
by Jon Kalb and Dave Abrahams
From the script-style intro:
UDT (putting down drink and looking deep into CODER’s eyes): By the way, in my destructor I throw if the file fails to close properly.
CODER: I can live with that. [...] I’ve always been a sucker for dangerous types.
FADE TO BLACK
NARRATOR (V.O.): We invite you to journey with us now, into the heart of darkness, where we seek out throwing destructors and stare Evil™ in the face. Will CODER survive his encounter with exceptional destruction? The answers lie beyond the frontiers of usual best practice…
By Blog Staff | Jul 23, 2012 04:14 PM | Tags: None
The C++11 update to the classic C++ Primer is almost here! Andy Koenig reports:
Barbara Moo shipped the completed text of the C++ Primer, Fifth Edition to the publisher on July 13. As far as I know, copies are already being printed; they should be on bookstore shelves by mid-August.
This book has been a major project for her for the past two years or so, and an all-consuming one since about the beginning of this year. I've spent a fair amount of time on it as well: reading drafts, making comments and suggestions, running test programs, and so on. As a result, I've had a pretty good idea of what she's been thinking about as she wrote the book, and I'm in a position not only to tell you what I've learned about her strategy, but also why I think her strategy is a sensible one