The Real Stroustrup Interview
Total Page:16
File Type:pdf, Size:1020Kb
“Any clod can have the facts, but having opinions is an art.” Charles McCabe, San Francisco Chronicle directly matches a sound design. How- ever, now is the time to experiment and The Real see which techniques will suit particular people, organizations, and projects. Much of The C++ Programming Language is devoted to these techniques Open Channel Stroustrup and the trade-offs they represent. The most visible aspects of what makes this progress feasible are the “new” major language facilities—tem- Interview plates, exceptions, runtime type infor- mation, and namespaces—and the new standard library. The minor improve- ments to language details are also impor- tant. We have had most of the facilities In Design and Evolution of C++ (Addi- available for years now, but haven’t been son Wesley, 1994), Bjarne Stroustrup able to rely on them where we needed argued that “a programming language portability and production quality. This is really a very tiny part of the world, has forced us to take suboptimal and as such, it ought not be taken too approaches to design and implementa- seriously. Keep a sense of proportion tion of our libraries and applications. and most importantly keep a sense of However, soon (this year) all major humor. Among major programming implementations will provide solid sup- languages, C++ is the richest source of port for Standard C++. puns and jokes. That is no accident.” From a language and programming- For the past few months, a hoax techniques perspective, the most signifi- interview between Stroustrup and cant change to C++ is the continuing Computer has been making the rounds The father of C++ increase in emphasis on statically verifi- in cyberspace. While we regret the inci- able type safety and the increased flexi- dent, it offers us a welcome oppor- explains why Standard bility of templates. Flexible templates are tunity to have the father of C++ share C++ isn’t just an object- necessary to make the emphasis on type his insights on Standard C++ and soft- oriented language. safety pay off in terms of elegance and ware development in general. We can efficiency. also attest to his continued sense of proportion and humor—he suggests C++ community in myriad direct and Feasibility of new techniques that the fictitious interview would have not-so-direct ways. Obviously, we will Computer: You say that “often what an been a much funnier parody had he get better implementations as compiler experienced C++ programmer has failed written it himself. providers start shifting attention from to notice over the years is not the intro- —Scott Hamilton, Computer catching up with the standards commit- duction of new features as such, but tee to quality-of-implementation issues. rather the changes in relationships STANDARD C++ This is already happening. between features that make fundamental Computer: ISO approved the Standard Standards-conforming implementa- new programming techniques feasible.” C++ in November 1997, and you pub- tions will prove a boon to tools and Could you give some examples of how lished a third edition of your The C++ library suppliers by providing a larger this might be applicable to Standard C++? Programming Language (Addison Wes- common platform to build for. Stroustrup: It is easy to study the rules of ley, 1997). How has C++ evolved over the The standard gives the programmer an overloading and of templates without past few years and what does the ISO opportunity to be more adventurous noticing that together they are one of the standard mean for the C++ community? with new techniques. Programming keys to elegant and efficient type-safe Stroustrup: It is great to finally have a styles that used to be unrealistic in pro- containers. This connection becomes complete, detailed, and stable definition duction code are becoming realistic obvious only when you look into the fun- of C++. This will be a great help to the propositions. Thus, more flexible, gen- damental algorithms that are common to eral, faster, and more maintainable code most uses of containers. Consider the can be written. program segment in Figure 1a. The first Editor: Will Tracz, Lockheed Martin Federal Naturally, we should keep cool and call of count() counts the number of Systems, MD 0210, Owego, NY 13827-3998; not indulge in orgies of “advanced” tech- occurrences of the value 7 in a vector of [email protected] niques. There are still no miracles, and integers. The second call of count() the best code is still the code that most counts the number of occurrences of the 110 Computer void f(vector<int>& vi, list<string>& ls) { int n1 = count(vi.begin(),vi.end(),7); int n2 = count(ls.begin(),ls.end(),”seven”); value “seven” in a list of strings. // ... There are ways of achieving this in } every language that allows overloading. (a) Given templates, we can write a single function that handles both calls, as template<class Iter, class T> shown in Figure 1b. This template will int count(Iter first, Iter last, T value) expand into optimal code for the two { calls. This count() is roughly equiva- int c = 0; lent to the standard-library count() so while (first!=last) { a user will not have to write it. The if (*first == value) c++; count() function itself relies on over- ++first; loading: Obviously, the equality opera- } tor == is overloaded for integers and return c; strings (with the obvious meaning). In } addition, * and ++ are overloaded to (b) mean “dereference” and “refer to next element” for the iterator types for vec- Figure 1. Changes in relationships between features can make fundamental new programming tors and lists; that is, * and ++ are given techniques feasible: (a) A count algorithm for counting the number of occurrences of the value the meaning they have for pointers. 7 in a vector of integers, as well as the number of occurrences of the value “seven” in a list of This definition of count() illustrates strings becomes (b) a single function that handles both calls using a template. a couple of the key techniques used in the containers and algorithms part of the C++ standard library. These techniques are template<class Iter, class Predicate> useful, and they are not trivially discov- int count_if(Iter first, Iter last, Predicate pred) ered given only the basic language rules. { Consider a slightly more advanced int c = 0; variation of the count() example while (first!=last) { shown in Figure 2a. Instead of counting if (pred(*first)) c++; occurrences of a value, count_if() ++first; counts the number of elements that ful- } fill a predicate. For example, we can return c; count the number of elements with a } value less than 7 as shown in Figure 2b. (a) Generalizing this technique to handle comparisons with any value of any type bool less_than_7(int i) { return i<7; } requires us to define a function object— that is, a class whose objects can be void f2(vector<int>& vi, list<int>& li) invoked like functions, as shown in { Figure 3a. The constructor stores a ref- int n1 = count_if(vi.begin(),vi.end(),less_than_7); erence to the value we want to compare int n2 = count_if(li.begin(),li.end(),less_than_7); against, as in Figure 3b, and the applica- // ... tion operator (operator()) does the } comparison. In other words, count the (b) number of elements in vi that have a value less than the integer 7, and count Figure 2. More advanced variation of the count example that (a) counts the number of elements the number of elements in ls that have that meet a predicate—for example, (b) the number of elements with a value less than 7. a value less than the string “seven.” It may be worth mentioning that the code generated is very efficient and in techniques for using templates and over- Language features are fundamentally particular does not use function calls—or loading—and that, of course, is part of boring in isolation and they can distract similar relatively slow operations—to the purpose of presenting the examples from good system building—only in the implement comparisons, fetching the here. However, the bottom line is that context of programming techniques do next element, and so forth. these techniques allow you to write effi- they come alive. Undoubtedly, this code will look cient, type-safe, and generic code. People strange to someone who is not a C++ pro- familiar with functional languages will The Standard Library grammer and even to C++ programmers note that these techniques are similar to Computer: How was the Standard who have not yet internalized the latest techniques pioneered in those languages. Library defined and what impact will it June 1998 111 template <class T> class Less_than { const T v; public: Less_than(const T& vv) :v(vv) {} // constructor bool operator()(const T& e) { return e<v; } }; matching, and graphics. Fortunately, these facilities are available commercially and/or (a) in the public domain. The standard library saves program- mers from having to reinvent the wheel. void f3(vector<int>& vi, list<string>& ls) In particular, standard containers allow { both novices and experts to program at int n1 = count_if(vi.begin(),vi.end(), a higher level. Consider the simple pro- Less_than<int>(7)); gram in Figure 4, which performs a sim- int n2 = count_if(ls.begin(),ls.end(), ple task simply. It does so without Less_than<string>(“seven”)); macros, without explicit memory man- // ... agement or other low-level language } facilities, and without resource limita- tions. In particular, if some joker presents (b) the program with a 30,000 character “first name” the program will faithfully Figure 3. To handle comparisons with any value of any type, we define (a) a class whose objects print it back at him without overflow or can be invoked like functions (function object), with the constructor storing (b) a reference to other errors.