I’ve finally started to look into the new features in C++11 and I thought it would be useful to jot down the highlights, for myself or anyone else who’s curious. Since there’s a lot of ground to cover, I’m going to look at each item in its own post — this one covers improvements to template declaration and instantiation.
This is the 5th of the 8 articles that currently make up the “C++11 Features” series.
As fancy as templates seem, they’re really just a type-safe version of pre-processor macros. That is, they do syntactic substitution instead of simple textual substitution. This is part of the reason they’re powerful, but also brings downsides.
One of the main drawbacks is related to compile time and code size, and this is really an interaction between the way templates work and the way C and C++ code is compiled. Because each source file is compiled into an independent object, and the compiler has no way to know in advance how these will be linked together, the compiler has to generate any template code which is used in that source file. Also, it’s possible to force instantiation by simply declaring each instantiated type:
template std::vector<int>;
template std::vector<double>;
This all works for a single object file, but what happens when you link these object files together? Well, if you’re lucky (read: using a halfway recent linker on a well-supported platform) then the code will be automatically collapsed together at link time. If you’re unlucky, you’ll get potentially many copies of each template instantiated — this will work, but leads to unnecessarily large compiled code, with resultant performance implications due to reduced caching potential.
Even if you’re lucky then compile times are still higher because the compiler has to generate those same template instantiations for each compilation unit, even if they’ll later be collapsed together during linking.
So, how does C++11 help here? Well, they’ve added a pretty simple construct which allows you to forward-declare template instantiations:
extern template std::vector<int>;
This is similar to a function prototype — it tells the compiler to assume that a definition of the template instantiation will be provided elsewhere so it doesn’t need to generate one. Of course, if no such instantiation exists then a link error will result.
This means that, for example, code which defines templated classes can use the
original C++ syntax above to create instantiations for common types in its
source file, and then declare them extern
in its header file, so clients of
the class need not generate their own. This could provide definite compile time
(and potentially code size) improvements for large projects when used with
common classes such as containers.
Templating is a great way to make code generically useful whilst maintaining the type safety which is one of the biggest advantages of a statically typed language. However, add a few levels of nesting and things can rapidly become cumbersome — consider the following loop declaration:
for (std::map<std::string, std::list<std::string> >::iterator it = myMap.begin;
it != myMap.end(); ++it) {
// ...
}
One way to cut down on this verbosity is to use typedef
:
typedef std::map<std::string, std::list<std::string> > MyMapType;
for (MyMapType::iterator it = myMap.begin(); it != myMap.end(); ++it) {
// ...
}
This works fine for template instantiations (i.e. with all template parameters
specified) but it’s not legal to do this for templates which have not been
instantiated in C++03. In C++11 this has now been allowed via a new use of the
using
keyword:
template <typename ListType>
using MapStringToListType = std::map<std::string, std::list<ListType>>;
In the example above the type MapStringToListType<std::list<int>>
can be used as
an alias for std::map<std::string, std::list<int>>
.
This corrects an omission which may have perhaps been only occasionally annoying, but in those cases it was particularly annoying. There are a few limitations, the major ones being that you can’t partially or fully specialise an alias template; and alias templates are never used as candidates for automatic template argument deduction.
Variadic functions have existed since the early days of the C programming
language. Probably the most well known example is the printf()
function with
the following prototype:
int printf(const char *fmt, ...);
The ellipsis in there denotes an arbitrary number of arguments follow and the compiler and standard library together provide runtime support for determining the number of arguments passed and iterating through them.
In C++11 this feature has been brought to templates:
template<typename... TYPES>
class Aggregate;
Instantiations of such templates can take any number of template arguments - for example:
Aggregate<int, float, std::map<std::string, int>> aggregateInstance;
The language doesn’t provide a mechanism to iterate over the template parameters, however, so often they’re used recursively. The example below shows how the ellipsis can be used to pass a variable number of template arguments into a function:
template<typename TYPE>
void print_stuff(TYPE arg)
{
std::cout << "Arg: " << arg << "\nEND" << std::endl;
}
template<typename FIRST, typename... REST>
void print_stuff(FIRST first_arg, REST... rest_args)
{
std::cout << "Arg: " << first_arg << "\n";
print_stuff(rest_args...);
}
I suspect the legitimate use-cases for these are fairly limited, but at least there’s now a certain amount of type-safety involved, which is more than can be said for traditional varargs functions1.
Finally, a teensy little change to the parse rules to address a long-standing annoyance of mine. In C++03 nested template instantiations required an annoying extra space to prevent the parser identifying the double-angle bracket as a bitshift operator:
std::vector<std::vector<int>> myVector; // Incorrect in C++03
std::vector<std::vector<int> > myVector; // Corrected version
In C++11 the parsing rules have been modified so that both of the above examples would be valid and equivalent. If, for some reason, the old behaviour is required then it can be forced with appropriate bracketing (but that seems unlikely!).
With the debatable exception of printf()
which is common enough
that some compilers (e.g. gcc) allow a varags function to be
flagged as in the same style as printf()
and do type checking
against the format string. This is the only such case of
type-safety in traditional varargs of which I’m aware, however. ↩