ISOCpp.org re-advertised the Channel9 videos from last year’s CppCon, so I thought I’d watch a couple. I started with Writing Good C++14. The slides are available here.
Stroustrup’s aim is to provide guidelines for writing simpler, cleaner C++ – then use this to spread the word of how Modern C++ can be so much better than archaic C++ idioms that have been superseded by new techniques.
But how to do it, because coding guidelines are frequently ignored or wrong. Telling what not to do is not enough – you need to do more than prohibit use of certain language features. Instead, build a set of what you should do – comprehensive, browsable, teachable guidelines.
High-level rules: philosophical approach to coding. And low-level rules: more specific, tangible rules that can be checked using tools (at least in the future) – each rule must have a rationale and an example as well as alternatives/exceptions/enforcement. And Guideline Support Library (GSL) for useful abstractions over messy/dangerous features – e.g. not_null, owner, array_view.
Result could be: productivity doubled (like a great programmer working on a modern codebase); less debugging by eliminating whole classes of errors – resource leaks, dangling pointers.
For example, dealing with range checking:
// Before
void f( int* p, int n ) // this is Stroustrup's least favourite interface!
{
p[7] = 9;
for ( int i = 0; i < n; ++i ) p[i] = 7;
}
// Guideline - better
void f( array_view<int> a)
{
a[7] = 9; // checkable against a.size() e.g. in debug mode
for (int x : a ) a = 7;
}
Example – dealing with null pointers:
// Before
void f( char* p )
{
if ( p == nullptr ) // is it necessary to check every time?
{
}
}
// Guideline - better
void f( not_null<char*> p )
{
// no longer needs to check, not_null<> cannot hold nullptr.
}
This looks pretty interesting – there’s a VS2015 plugin to run the GSL tools too. This featured in the follow-up presentation by Herb Sutter:
