I recently purchased Stroustrup’s C++11 book [1], after borrowing it a number of times from the Markham public library (it’s very popular, and only offered for short term loan) . Here are some notes of some bits and pieces that were new to me for this round of reading.
nothrow new
In DB2 we used to have to compile with -fcheck-new or similar, because we had lots of code that predated new throwing on error (c++98). There is a form of new that explicitly doesn’t throw:
void * operator new( size_t sz, const nothrow_t &) noexcept ;
I don’t know if this was introduced in c++11. If this was a c++98 addition, then it should be used in almost all the codebases new calls. When I left DB2 there were still some platform compilers (i.e. AIX xlC which doesn’t use the clang front end like linuxppcle64 xlC) that were not c++11 capable, so if this explicit nothrow isn’t c++98, it probably can’t be used.
Unnamed function parameters
It is common to see function prototypes without named parameters, such as
I did not realize that is also possible in the function definition, as in code like the following where a parameter has been dropped or left as a placeholder for future use
void foo( int x, int )
{
printf( "%d\n", x ) ;
}
Not naming the parameter is probably a good way to get rid of unused parameter warnings.
This is very likely not a c++11 addition. I just didn’t realize the language allowed for it, and had never seen it done.
No return attribute
Looks like __attribute__ extensions are being baked right into the language, as in
[[noreturn]] void exit( int ) ;
I wonder if this is also in the plan for C?
Thread safe static constructors
C++11 explicitly requires static variable constructors are initialized using a “call-once” mechanism
class x
{
public:
x() ;
} ;
void foo( void )
{
static x v() ;
}
Here there is no data race if foo() is executed concurrently in a number of threads. I remember seeing DB2 code that did this (and opening a defect to have it “fixed”), since I had no idea if it would work. We didn’t (and couldn’t yet) use -std=c++11, so it’s anybody’s guess what that does without that option and on older pre c++11 compilers.
Implied type initializer lists.
In a previous post I mentioned the c++11 uniform initialization syntax, but the basic idea is that is instead of
or
c++11 now allows
Here the variables are initialized with values 1, and 0 (the default). The motivation for this was to provide an initializer syntax that could be used with container classes. Here’s another variation on the initializer list initialization
int x = int{} ;
int y = int{3} ;
which can be reduced to
int x = {} ;
int y = {3} ;
where the types of the lists are implied. I don’t see much value add to use this equals-list syntax in the examples above. Where this might be useful is in templated code to provide defaults
template <typename T>
void foo( T x, T v = {} ) ;
Runtime values for default arguments.
I don’t know if this is new to C++11, but the book points out that default arguments can be runtime determined values. Initially, my thought on this was that it is good that is not well known, since it would be confusing. I did however, come up with a scenerio where this could be useful. I wrote some code like the following the other day
extern bool g ;
inline int foo( )
{
int res = 0 ;
if ( g )
{
// first option
}
else
{
// second option
}
return res ;
}
The global g was precomputed at the shared library startup point (effectively const without being marked so). My unit test of this code modified the value of g, which was a hack and I admit ugly. It looked like
BOOST_AUTO_TEST_CASE( basicTest )
{
for ( auto b : {false, true} )
{
g = b ;
int res = foo() ;
BOOST_REQUIRE( res >= 0 ) ;
}
}
This has a side effect of potentially changing the global. A different way to do this would have been
extern bool g ;
inline int foo( bool internalOverrideOfGlobalForTesting = g )
{
int res = 0 ;
if ( internalOverrideOfGlobalForTesting )
{
// first option
}
else
{
// second option
}
return res ;
}
The test code could then be rewritten as
BOOST_AUTO_TEST_CASE( basicTest )
{
for ( auto b : {false, true} )
{
int res = foo( b ) ;
BOOST_REQUIRE( res >= 0 ) ;
}
}
This doesn’t touch the global (an internal value), but still would have allowed for testing both codepaths. The fact that this “feature” exists may not actually be in this case, since my interface was a C interface. Does a
noexcept
Functions that intend to provide a C interface can use the noexcept keyword. That allows the compiler to enforce the fact that such functions should provide a firewall that doesn’t let any exceptions through. Example:
// foo.h
#if defined __cplusplus
#define EXTERNC extern "C"
#define NOEXCEPT noexcept
#else
#define EXTERNC
#define NOEXCEPT
#endif
EXTERNC void foo(void) NOEXCEPT ;
// foo.cc
#include "foo.h"
int foo( void ) NOEXCEPT
{
int rc = 0 ;
try {
//
}
catch ( ... )
{
// handle error
rc = 1 ;
}
return rc ;
}
If foo does not catch all exceptions, then the use of noexcept will drive std::terminate(), like a throw from a destructor does on some platforms.
References
[1] Bjarne Stroustrup. The C++ Programming Language, 4th Edition. Addison-Wesley, 2014.
Like this:
Like Loading...