This presentation from Stephan Lavavej could have been entitled “Compiler Knows Best” – he rockets through numerous examples where the advice is to write less code and let the compiler do the right thing.
Here’s a slide of one of the key messages:
Other highlights for me:
- decltype(auto) coming in C++14 looks very cool (eliminates duplicate code when using auto with decltype for late-specified return values)
- Don’t call std::make_pair( x, y ) – the T, U are inferred by template argument deduction, so should always be left out (unless you want to force a type conversion when T != typeof(x) e.g. short -> long
- Use nullptr because it is convertable to all pointer types, but 0 and NULL are always type int
I love the new features of C++11 and couldn’t have written some of the cool libraries I’m working on without them. But – presentations like this make me concerned for the complexity of the language and the steep learning curve that even experienced C++ hires will have to climb.
Stephan Lavavej has submitted a proposal to the C++ Standards committee for make_unique (the std::unique_ptr equivalent to std::make_shared for std::shared_ptr).
make_unique’s presence in the Standard Library will have several wonderful consequences. It will be possible to teach users “never say
new/delete /new/delete” without disclaimers. Additionally,
make_unique shares two advantages with
make_shared (excluding the third advantage, increased efficiency). First,
unique_ptr<LongTypeName> up(new LongTypeName(args)) must mention
LongTypeName twice, while
auto up = make_unique<LongTypeName>(args) mentions it once. Second,
make_unique prevents the unspecified-evaluation-order leak triggered by expressions like
foo(unique_ptr<X>(new X), unique_ptr<Y>(new Y)). (Following the advice “never say
new” is simpler than “never say
new, unless you immediately give it to a named
It’s a really useful utility as demonstrated in this video.
Introductory video on the Visual Studio 2012 C++ Compiler November CTP (download the CTP here):
A special episode in which Stephan takes a look at the latest C++11 features that were just added to the Visual C++ compiler:
Raw string literals
Explicit conversion operators
Default template arguments for function templates
Click to watch the video