Grok The New Features in Standard C++ ByDanny Kalev 2004-08-16
Article Rating: / 0
Rate This Article:
Add This Article To:
The definition of C++ isn't standing still. Discover how the standard is changing—and what effect it's going to have on your code.
Believe it or not, the current C++ standard (formally known as C++98) is already six years old. Considering the time that has elapsed since its finalization in 1996, it is even older. In the meantime, the software world has changed considerably: XML, merely an experimental language in the mid 1990s, has turned into a de-facto standard; projects that combine multiple programming languages are now commonplace; and pure object-oriented programming has lost its luster in favor of other paradigms.
To keep up with the pace of today's software world, the C++ Standard Library is currently being updated and enhanced. I will introduce you to some of its new features.
ADVERTISEMENT
Regular Expressions
A regular expression (regex) specifies a pattern for selecting specific strings from a text corpus. Thus far, C++ programmers had to make do with third-party libraries and the regex API defined by POSIX. Alas, those libraries support only char * strings and suffer from other limitations. The new C++ regex library, based on the boost::regex library, supports char * , wchar_t *, string, and wstring. It consists of a pair of classes and a set of algorithms that operate on objects of those classes. Regular expressions are represented as objects of type basic_regex. Matches against a regular expression are represented as match_results objects. The algorithms regex_match and regex_search take a regular expression and a string, and fill in a match_results object if a match is found. Search and replace operations use the algorithm regex_replace which produces a new string.
Function Object Wrapper
C++0X adds a new class template called function to the header <functional>. function generalizes the notion of a function pointer. It serves as a wrapper of freestanding functions, member functions, and function objects, while maintaining the uniform syntax and semantics. When using an instance of function to call a function, you don't need to know the target function's precise signature. Any target function will match, as long its parameter(s) and return type are convertible to the corresponding parameters and return type of the function object. In the following example, the same function object is used for invoking two freestanding functions and a function object's operator(), all of which have compatible signatures:
int add(int m, int n) {return m+n;}
bool adjacent(int m, int n) {return m == n-1 || m == n+1;}
class test {
vector vals;
public:
bool operator()(int sz, int val) //signature as of adjacent
{
bool wasempty=vals.empty();
vals.resize(sz,val);
return wasempty;
}
};
function f=&add;
int res=f(3,3); //ok, calls add(3,3)
f=&adjacent; //OK, return type implicitly converted
bool b= f(5,5); // calls adjacent(5,5);
test t; // a functor with a compatible signature
f=ref(t); // bind f to a reference to the said functor
f(3,3); //ok, calls t.operator()(3,3)
A Soapbox Note: Naming a standard class function is a questionable decision (namespaces notwithstanding). This token is used extensively in production systems and thus will cause existing code to break. It will also result in misunderstandings in textbooks and lectures due to the ambiguity of this name. Does function mean a piece of code that can be invoked, or is it the name of the proposed class? Likewise, does function object mean a functor or an instance of class function? It's not too late to consider a different name, say function_t.
Tuples
Many programming languages (such as Python, Eiffel, and ML) have tuple types. A tuple is a fixed-size heterogeneous container that packs an arbitrary number of objects of various types into a single object. A tuple can be useful in the following cases:
Serving a the return type of a function that needs to have multiple return types
Packing related types or objects, e.g., a function's parameters and arguments, in a single tuple
Simultaneous assignment of multiple values
Unlike STL containers such as vector, a tuple's size is fixed and determined at compile time. It may range between 0 to 10 elements (according to the current proposal).
The class template tuple and its related helper functions are declared in the new header <tuple>. The following examples instantiates a tuple object, fills it with elements of various types and assigns it to another tuple object:
New overloaded versions of the << and >> operators support input and output of tuples. Output is performed by invoking operator << for each element. The following line:
cout<<make_tuple(1, 'a', "Test")<<endl;
displays:
(1 a Test)
Additional Changes and Extensions
C++0X also incorporates several fixes for omissions in C++98.
vector's Contiguity: Although, in practice, all current implementation store vector's elements contiguously, there's no such requirement in C++98. C++0X fixes this by stating this requirement explicitly.
Hashed Containers: Another noticeably lacking feature in C++98 is hashed containers. Such containers were indeed proposed in 1995 but they arrived too late to be incorporated into the standard.
Several vendors have since provided hashed containers as a non-standard extension. C++0X fills this gap now. The C++ standards committee decided to give these containers a different collective name, Unordered Associative Containers, to distinguish them from existing non-standard implementations. The Unordered Associative Containers are: unordered_set, unordered_map, unordered_multiset,
and unordered_multimap. They are declared in new headers: <unordered_set>, <unordered_map>,
<unordered_multiset>, and <unordered_multimap>,
respectively.
Mathematical Special Functions: C++0X incorporates into the <cmath> header several new mathematical special functions, which are mostly used in scientific and engineering communities. These include cylindrical Bessel functions, cylindrical Neumann functions, and other related functions.
New Smart Pointer Classes: C++98 defines only one smart pointer class, auto_ptr, which implements strict ownership semantics and therefore can't be used in Standard Library containers. C++0X introduces new smart pointer classes: shared_ptr and weak_ptr. The new smart pointer classes can (at last!) be used as elements of STL containers. Their design reflects more than a decade of refinement
and usage and they can be adjusted to various frameworks such as COM and garbage-collected environments.
Actions Speak Louder Than Words
Contrary to the tremendous hype coming from lobbyists of newer programming languages, C++ is certainly not an "old" language; in terms of innovation and original design, it's light years ahead of the "new languages", particularly with respect to generic programming. Indeed, C++ still lacks a few key features, such as concurrency, reflection, and an XML parser. However, incorporating these features would require new libraries (two of which are already available on Boost) with minimal core language changes, if any.
Disclaimer: All the code samples, class names, algorithms, and other design and implementation details used here are subjected to changes, as these proposals are still being
drafted and reviewed by the C++ standards committee. (If you want to buy an electronic copy, you can find it at the official NCITS standards shop.)
Presently, the committee is reviewing the features presented here and many others. Hopefully, the next version of the C++ standard will offer all the facilities that a general purpose programming language of the 21st century should have.
Danny Kalev is a certified system analyst and software engineer specializing in C++ and theoretical aspects of formal languages. He was a member of the C++ standards committee in
1997-2000. He has an MA in general linguistics from the University of Tel Aviv. In his spare time, he listens to classical music, reads Victorian literature, and explores natural languages such as Hittite, Basque, and (most recently) Gaelic. Danny occasionally gives lectures about programming languages and applied linguistics at academic institutes.