New features in the GCC versions 4.3 and 4.4
New Builder
Recent versions of the GNU compiler include new features from the next C++ standard.
The new C++ standard, which is informally known as C++0x, is still in the process of being hashed out, but many features of the next generation C++ have already made their way into the GNU compiler (GCC) versions 4.3 and 4.4. You can enable these features by entering the -std=c++0x option.
Many of these new features are mature enough to use without worries. Users of the influential Boost libraries [1] will be familiar with most of the changes because a fair share of the new standard is based on Boost. The technical details are available in the draft version of the C++ standard [2] and in the "Draft Technical Report on C++ Library Extensions" (TR1) [3]. Read on for a look at some of the most important changes in recent versions of GCC.
New Functions
The C99 standard substantially raised the number of mathematical functions available. It stands to reason that all of these C functions should be available in C++. The cmath library under the new C++ standard incorporates the full set of C99 functions (Table 1), while defining many additional mathematical functions (Table 2). The new C++ standard also mandates that new C libraries, such as stdint.h (fixed-size integers) or fenv.h (direct access to the floating point unit), must be available in C++. The C++ names for these libraries are cstdint, cfenv, and so on. The definitions are all bundled in the std namespace.
New Containers
Several new containers also inhabit the new standard, starting with the hash-based variants of the associative containers set, multiset, and map. These containers were originally planned for the older standard, but the developers dropped them for want of time. The new standard includes the unordered_set and unordered_map libraries, which define the hash-based containers unordered_set, unordered_multiset, and unordered_map. The unordered name component indicates that a comparative operator is not needed in order to use these containers. The unordered_set class definition is as follows:
template<class Value, class Hash=hash<Value>, class Pred=std::equal_to<Value>, class Alloc=std::allocator<Value> > class unorderd_set { ... }
With the template parameters, you can select the allocator (as with any standard container), specify the comparative predicate (this defaults to ==), and specify the hash function. If no specific information concerning the data is available, the default settings are a useful choice. Hash-based containers are faster than their tree-based counterparts (Table 3). The array (a lightweight class for a C array) and forward_list (a singly linked list) classes are also new.
Intelligent Pointers
The current C++ standard includes auto_ptr, a pointer that deletes an object automatically when it loses its value. The auto_ptr class has many restrictions. Each object can have only one owner, and strange things can happen in copying operations. More specifically, you cannot store an auto_ptr in a standard container. Effectively, auto_ptr causes more problems than it solves. The new C++ standard gets rid of auto_ptr and introduces improved, intelligent pointers. The new intelligent pointers are:
- unique_ptr, which is most similar to the legacy auto_ptr. The object belongs exclusively to the pointer. unique_ptr gives programmers an exception-proof approach to storing a pointer in a function.
- shared_ptr is a universally deployable, intelligent pointer. Multiple shared_ptr can share an object. If the programmer deletes the last shared_ptr pointing to an object, the destructor for the pointer calls the destructor for the object.
- weak_ptr monitors the objects managed by shared_ptr. The weak_ptr has no rights for the object. Thus, it is up to the programmer to ensure that the object stays alive while the pointer is alive. The weak_ptr and the shared_ptr are identical to the intelligent pointers of the Boost libraries.
The intelligent pointer collection should lead to more efficient programming as developers learn the nuances of the various new options.