Comparison and Inheritance -- Andrew Koenig
Comparison and Inheritance
by Andrew Koenig
We continue last week's discussion of comparison functions by thinking about how to compare objects from different parts of an inheritance hierarchy...
June 17-20, Folkestone, UK
September 12-18, Aurora, CO, USA
November 16-21, Búzios, Rio De Janeiro, Brazil
November 26-28, Berlin, Germany
By Blog Staff | Feb 25, 2013 03:11 PM | Tags: intermediate
Comparison and Inheritance
by Andrew Koenig
We continue last week's discussion of comparison functions by thinking about how to compare objects from different parts of an inheritance hierarchy...
By Blog Staff | Feb 21, 2013 09:25 AM | Tags: intermediate
To accept a functor as a parameter, when should you:
std::function, which adds an indirection, vs.template<class Func> and accept a Func, which can bind directly to whatever is passed?std::function vs template
Thanks to C++11 we received the
std::functionfamily of functor wrappers. Unfortunately, I keep hearing [...] that they are horribly slow. [... Is the right recommendation] thatfunctions can be used as de facto standard of passing functors, and in places where high performance is expected templates should be used?
By Blog Staff | Feb 18, 2013 05:32 PM | Tags: intermediate basics
Quick A: Yes. Another way that modern C++ is safer and simpler.
(If you don't know what the safe-bool idiom is, don't worry. It's a workaround that's now obsolete.)
Xeo asked:
Is the safe-bool idiom obsolete in C++11?
This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple
explicit operator bool() const;... Is our assumption in the title correct? I hope we didn't overlook any potential drawbacks.
By Blog Staff | Feb 15, 2013 10:38 AM | Tags: intermediate basics
Andrzej continues this month with more interesting thoughts on preconditions.
Preconditions, Part 2
by Andrzej Krzemieński
In this post I will continue sharing my thoughts on preconditions. It will cover some philosophy behind the concept of preconditions (and bugs), and investigate the possibility of employing the compiler to verify some preconditions. Many people provided a useful feedback on my previous post. I will also try to incorporate it into this post.
Note that this article diverges from recommended practice in one way... it hints at the idea of throwing exceptions to report precondition violations. Instead, per C++ Coding Standards and other established guidance, prefer to use assertions to check preconditions: precondition violations are just bugs in the caller's code that should be caught at test time, assertions cause no overhead in production, and assertions fire immediately at the line of code that contains the bug without losing the call stack and other local context. Using assertions is still considered to be a best practice.
By Blog Staff | Feb 13, 2013 06:41 PM | Tags: intermediate advanced
Here's a recent highlight from the pre-Portland mailing that you might have missed:
Open and Efficient Type Switch for C++
Yuriy Solodkyy, Gabriel Dos Reis, Bjarne Stroustrup
... we implement a type switch construct as an ISO C++11 library, called Mach7. This library-only implementation provides concise notation and outperforms the visitor design pattern. ... For closed sets of types, its performance roughly equals equivalent code in functional languages, such as OCaml and Haskell.
C++ is a powerful library-building language. Whenever possible, we prefer to add new functionality as a library rather than in the language. This is an excellent example of where a C++ library-only solution can get equivalent performance to the language support included in some popular functional languages.
By don_tait | Feb 12, 2013 01:31 PM | Tags: intermediate basics
KDAB is now offering three-day training courses in C++11.
What’s New in C++11?
This three-day training teaches everything about the new C++ standard, C++11.
C++11 will become more and more important in the C++ ecosystem, eventually becoming the most prevalent version. Every professional developer should invest in learning the new language version and try introducing its benefits into projects. And for good reasons: C++11 brings a large range of new features that makes development safer, faster, easier and more fun. Once you have tried features like lambda functions, range-based for loops, the auto keyword and the new initialization syntax, you won't want to go back. In addition to that, many more advanced features like variadic templates, rvalue reference and of course the new standard library additions like multithreading classes, smart pointers, regular expressions and new containers and algorithms complete the picture.
During the training day at Qt DevDays 2012 in Berlin, KDAB engineer Marc Mutz, presented some the most important C++11 features using parts of the material from this course. This was very well attended, receiving positive feedback.
Our full training lasts for three days and covers a wide range of topics, it goes in-depth and provides time to show C++11 examples as well as allowing participants to go hands-on and trying out C++11 themselves in exercise projects.
See the course description for more details about the content.
To view our schedule and to book your place for our next C++11 trainings go to: www.kdab.com/schedule/
By Blog Staff | Feb 11, 2013 11:42 AM | Tags: intermediate
Herb Sutter's biggest and deepest talk at C++ and Beyond 2012 is now online:
atomic<> Weapons: The C++ Memory Model and Modern Hardware
by Herb Sutter
This session in one word: Deep.
It's a session that includes topics I've publicly said for years is Stuff You Shouldn't Need To Know and I Just Won't Teach, but it's becoming achingly clear that people do need to know about it. Achingly, heartbreakingly clear, because some hardware incents you to pull out the big guns to achieve top performance, and C++ programmers just are so addicted to full performance that they'll reach for the big red levers with the flashing warning lights. Since we can't keep people from pulling the big red levers, we'd better document the A to Z of what the levers actually do, so that people don't SCRAM unless they really, really, really meant to.
Topics Covered:
- The facts: The C++11 memory model and what it requires you to do to make sure your code is correct and stays correct. We'll include clear answers to several FAQs: "how do the compiler and hardware cooperate to remember how to respect these rules?", "what is a race condition?", and the ageless one-hand-clapping question "how is a race condition like a debugger?"
- The tools: The deep interrelationships and fundamental tradeoffs among mutexes, atomics, and fences/barriers. I'll try to convince you why standalone memory barriers are bad, and why barriers should always be associated with a specific load or store.
- The unspeakables: I'll grudgingly and reluctantly talk about the Thing I Said I'd Never Teach That Programmers Should Never Need To Now: relaxed atomics. Don't use them! If you can avoid it. But here's what you need to know, even though it would be nice if you didn't need to know it.
- The rapidly-changing hardware reality: How locks and atomics map to hardware instructions on ARM and x86/x64, and throw in POWER and Itanium for good measure – and I'll cover how and why the answers are actually different last year and this year, and how they will likely be different again a few years from now. We'll cover how the latest CPU and GPU hardware memory models are rapidly evolving, and how this directly affects C++ programmers.
Herb adds on his blog:
Note: This is about the basic structure and tools, not how to write lock-free algorithms using atomics. That next-level topic may be on deck for this year’s C++ and Beyond in December, we’ll see...
By Blog Staff | Feb 11, 2013 10:41 AM | Tags: intermediate basics advanced
The final dates and location are now set for C++ and Beyond 2013 with Scott Meyers, Herb Sutter, and Andrei Alexandrescu:
December 9-12, 2013 in beautiful Snoqualmie, Washington, USA.
From Scott Meyers' announcement:
About a month ago, I posted tentative dates for C&B 2013. I cautioned that there was no contract yet, and I’m glad I did, because shortly thereafter we discovered an off-by-one scheduling snafu. As a result, the dates are not the ones I posted earlier, they’re a day later: Monday evening, December 9, through Thursday, December 12.The inital C&B in 2010 was held at the Salish Lodge and Spa in Snoqualmie, Washington, USA. In 2011, we had a larger group in a larger venue, and last year we bumped up the numbers again. Growth was ours, it seemed, but we sensed that C&B was looking more like a conventional conference and less like the unique event we had originally envisioned. For 2013, we decided to return to our roots, both geographically and organizationally.
C&B 2013 will return to the Salish Lodge and Spa in Snoqualmie, Washington (not far from Seattle). Enrollment will again be limited to the capacity of the ballroom (~64 attendees). Scott will again lead lunchtime walks. Evenings will again feature free-form “hang out with the speakers” sessions. Hotel guestrooms will again boast fireplaces, whirlpool tubs for two, and one whopping big waterfall just steps from the front door. If you were part of C&B 2010, you know what I’m talking about. If you weren’t, ask around: you’ll wish you had been.
We’ll announce more details when they’ve been finalized, including when registration for C&B 2013 will begin. In the meantime, reserve December 9-12 for C++ and Beyond 2013 in Snoqualmie, Washington, USA.
By Blog Staff | Feb 5, 2013 02:28 PM | Tags: intermediate
A short useful Q&A about how two C++ features interact:
C++11 Delegated Constructor Pure Virtual Method & Function Calls -- Dangers?
In C++ 11, what are the dangers of invoking Pure Virtual functions in a class' constructor, during construction, but after the class/object has been "fully constructed" via constructor delegation?
...
By Blog Staff | Feb 5, 2013 11:31 AM | Tags: intermediate advanced
Two advanced talks by Stephan T. Lavavej (aka STL) are now available, the second being posted today:
Core C++, 7 of N
In Part 7, STL teaches us about Usual Arithmetic Conversions, Template Metaprogramming (TMP), and shares some of the Visual C++ STL internal implementation (some of it not yet released). Many of you have asked for some treatment of TMP and STL delivers!
Core C++, 8 of N
In part 8, STL digs into the do-while loop, casts, one definition rule (ODR), and his variadic template array sorter. There is a lot of information in this episode, so get comfortable, tune in, and learn.