Cover picture

C++ Solutions

Companion to The C++ Programming Language, Third Edition

 


Stats
Official Site     http://www.awprofessional.com
Length     Just under 300 pages, softbound
Price     $30 and under
Publisher     Addison-Wesley
ISBN     0-201-30965-3


What others say...
"C++ Solutions by David Vandevoorde turns the best C++ guidebook into a better one"
                   — Danny Kalev for Dev-X
"A book worthy of your study if you are anything less than a C++ expert"
                   — Francis Glassborow for ACCU
"Vandevoorde has produced the perfect adjunct to Stroustrup's third edition of The C++ Programming Language"
                   — Peter H. Salus in ;login: (December 1998)


Overview
This book is a companion to the book that is widely recognized as "the C++ bible": The C++ Programming Language (third edition) by Bjarne Stroustrup. If you're a programmer, Bjarne's book comes very highly recommended.
My own book selects a little over 120 of the exercises proposed by Bjarne, and shows how they could be solved. Besides discussion of the source code, I have tried to convey a sense on "how to get there" and "what are alternatives?" Many exercises come with a discussion of maintainability and/or performance (I have gotten very positive feedback on the inclusion of sample benchmark numbers). They also extensively cross-reference Bjarne's book, but some of the more subtle language issues are often explained in the text for reading convenience.
You will notice that the organization of the chapters follows Bjarne's, but I do repeat the exercise statements (of course). That makes the book somewhat stand-alone, but I think it really benefits a lot from having The C++ Programming Language by its side. Note in particular that my book is not a reference or a complete tutorial (whereas Bjarne's is). Instead, I think it's a good way to practice C++ skills and compare ideas.
I have tried to select exercises covering a broad range of topics and ranging from the very basic to the pretty advanced. Some exercises are solved in half a page, others require over a dozen pages. Often I exploit the new power of the C++ standard library, but many core language idioms are illustrated as well. The book was written in 1997 and early 1998 as the ISO/ANSI C++ committee wrapped up its work. As a result, you'll find that the code fragments conform to that standard. One of the early chapters gives some suggestions on how to deal with C++ compilers that are not quite up to the standard yet, and several exercises that use some of the newer features mention that too.
On-line book reviews are available from the ACCU (this UK site has many good resources for programmers) and from Dev-X (membership required, but it is free.) A written review appeared in ";login:". (Yes, all reviews are flattering but I swear I didn't bribe or threaten anyone ;-)
Source code is provided on the Addison-Wesley site (free). You can also order the book there.
Danny Kalev wrote a very informative review of my book for Dev-X. In his review he noted that I only cover a selection of all the exercises in Bjarne's book, and in particular that I had not elucidated his favourite puzzle. So I will do that below...


An extra solution...
Exercise 15.8
Write a standards-conforming C++ program containing a sequence of at least ten different consecutive keywords not separated by identifiers, operators, punctutation characters, etc.
One way of interpreting this question is to say that alternative tokens such as bit_or are keywords. If we could in addition repeat such tokens, you would quickly come up with arbitrarily long sequences of keywords:
int main() {
   bool result = true and true and true
                 and true and not false;
}
Many may consider this cheating either on the grounds that a keyword is being repeated or that it is in fact not a keyword but an "alternative token" (the distinction is thin however, and you will note that Bjarne treats such tokens as keywords in par. A.2.) Another, perhaps more obscure, repeatable keyword (not an alternative token this time!) is sizeof:
int main() {
   return sizeof sizeof sizeof sizeof
          sizeof sizeof sizeof sizeof true;
}
Coming up with an uninterrupted sequence of distinct, honest-to-goodness keywords is a little harder. A good way to start is to stare at the table of all keywords (see e.g. par. A.2 in Bjarne's book). The most interesting candidate keywords are those that are not required to be surrounded by punctuation: const (par. 5.4), inline (par. 7.1.1) and new (par 6.2.6) could potentially be useful. On the other hand static_cast (par. 6.2.7) or private (par. 15.3) are less flexible because they are always followed by a non-keyword token ('<' and ':' respectively.)
Declarations can have a fair number of keywords. Here is a good starting point:
signed long int const volatile a;
Maybe I hear you say "You can add extern to that" (par. 9.2) or perhaps "Make it the return-type of a function and add qualifiers"? Good idea:
extern inline signed long int const volatile f();
Ah, and we could explicitly instantiate a template (par. C.13.10)... and it could be an operator (chap. 11):
template 
extern inline signed long int const volatile operator*(X&);
Nine keywords... very close...
In fact, if you allow "alternative tokens", you can have ten!
template
extern inline signed long int const volatile
operator and(X&, Y&);
So, mission accomplished as far as the spirit of the exercise goes, but for brainteasing's sake, let's look for a solution not involving alternative tokens. Some expressions can contain little pieces of declaration-like items (types). The typeid (par. 15.4.4) and sizeof (par. 4.6) operators are like that, but they require parentheses. The new operator (par. 6.2.6) does not have that restriction, so that is useful. Remember also that the sizeof operator doesn't require parentheses either if its argument is an expression instead of a type. Combining the two, we get:
sizeof new signed long int const volatile();
Seven only, but the expression can be grown with the throw operator (par. 8.3.1), so we are eight!
After that it may not be possible to grow the expression anymore, but some statements (par. 6.3) have keywords that are followed by expressions without intervening punctuation. If you look closely at the table in par. 6.3 of The C++ Programming Language, you will find that the do and else keywords are like that. So our final answer is:
int main() {
   if (false) {
   } else do
      throw sizeof new signed long int const volatile();
   while (false);
}
Clearly, this is not very useful code and indeed it is fair to say that useful code will never need to have ten consecutive distinct keywords in C++. However, this is a great bit of trivia to impress your friends and/or colleagues, and coming up with the solution got me to explore the C++ grammar in some more detail. 
Ah yes, and you cannot do this in C ;-)

Finally, we should mention the following clever trick sent in by “Prince Vijay Daniel”:

#define keywords this unsigned class struct \
   enum template if for namespace else do return \
   throw sizeof new signed long
We could list all the C++ keywords this way. However, we should also note that identifiers only become keywords after preprocessing, and therefore this latter solution does not quite fit the spirit of the exercise.
Many Thanks to Bjarne Stroustrup, Andrew Koenig and “Gargantua Blargg” for their suggestions and corrections.

The eleven distinct wholesome keywords challenge has been solved. “Gargantua Blargg” not only came up with that answer, but also shows how you can have twenty (yes, 20) different "extended" keywords placed in a valid C++ sequence ("extended" meaning that alternative tokens are included.) And Anatoli Tubman upped that to 24 keywords...
So now the new challenge is for twelve classic keywords or 25 extended keywords.
E-mail me if you know!



Copyright © 1995-2007 by David Vandevoorde