The last installment of my C/C++ Users' Journal "Common Knowledge"
column is now available on the CUJ Online Experts Forum at
http://www.cuj.com/documents/s=8906/cujexp0311dewhurst/
Unfinished Business
-------------------
All good things must come to an end and so, coincidentally, must this
column. In this last installment of Common Knowledge we'll look at
combining some template metaprogramming odds and ends that have been
kicking around in my "to think about sometime" folder to see if we can
come up with a reasonable framework for ad hoc type manipulation. We'll
fail, but it'll be fun to make the attempt. Along the way we'll consider
the creative use of rocks in automotive maintenance, type translation to
facilitate compile-time manipulation, un-instantiation of class
templates, giving the STL allocator "rebind" mechanism the boot,
typelist meta-generic algorithms, meta-predicate adaptation, and the
importance of having fun.
____________________________________
New Once, Weakly: Typelist Meta-Algorithms
The latest edition of Once, Weakly applies the STL formalism to the
manipulation of typelists. We develop algortithms to partition, sort,
and transform typelists (at compile time), using appropriate
meta-predicates and meta-comparators. We also demonstrate how to write
meta-function adapters that allow the modification and composition of
meta-predicates and comparators. See
http://www.semantics.org/weekly.html. Source code for the typelist
meta-algorithms and adapters is available at
http://www.semantics.org/code.html.
_______________________
C++ Gotchas in Hungarian!
C++ Gotchas is now available in Hungarian from Kiskapu! My
Hungarian/C++ expert (who chooses to remain anonymous) has told me that
title should have been translated as "C++ csapdák" (Traps of C++) rather
than "C++ hibaelhárító" (C++ Troubleshooter), but I'm quite pleased with
the book's appearance and sheer volume of diacritical marks. Read more
about it (in Hungarian) at
http://www.kiskapu.hu/main.php?SHOW_BODY=termekinfo&TERMEKINFO_ID=121665401.
Best regards,
Steve
_________________________
C++ GOTCHAS IN TURKISH
C++ Gotchas is now available in Turkish, and is going for the impressive
sounding sum of 13 million Turkish Lira. However, if you act now you
can get it from the publisher for only 10,400,000 TL. See
http://www.semantics.org/cpp_gotchas/index.html
_________________________
SD BEST PRACTICES
I'll be speaking at Software Development Best Practices in Boston,
September 15-18 (see http://www.sdexpo.com). This conference is a bit
of a departure for me, as I'll be treating somewhat "softer" issues than
I usually do, focussing on the human/sociological side of software
development and maintenance.
My first talk will be part of the "Design & Architecture" track:
EFFECTIVE USE OF PATTERN AND IDIOM
This class presents an overview of the practical use of design patterns
and idiom in the production of well structured, well documented, and
maintainable software. We will also discuss when it makes sense to
abandon idiom for a non-standard approach to problem resolution. The
class is primarily at the design level, although some code examples will
be presented in C++.
My second talk will be part of the "Build and Deploy" track:
SLEEPERS AND TIME BOMBS
Some coding and design gotchas are obvious because they cause
compilation errors. Others are uncovered during unit or system
testing. However, sometimes code that is initially correct can become
invisibly and undetectable erroneous during feature addition and
refactoring. These categories of gotchas can be labeled as "sleepers,"
that are just waiting for a remote modification to set them into
destructive action, or "time bombs," that present no difficulty for a
time, but suddenly explode into a major problem. This class discusses a
variety of these unpleasant gotchas, ranging from low-level coding
problems to project mismanagement. Code examples are presented in C++,
but the underlying principles apply to any implementation language.
I'll also be hosting a lunchtime roundtable in the "People, Projects &
Teams" track on:
WHAT DOES A C++ PROGRAMMER HAVE TO KNOW, AND HOW LONG WILL IT TAKE?
If you have a hard time associating speed with maturity, or on the other
hand, if this seems perfectly logical to you, come to this roundtable
discussion to share your point of view.
I can only assume that this rather surreal description was manufactured
by the organizers to punish me for not getting my abstract to them on
time. In any case, I mean to engage in a concrete discussion of the
requirements for both minimal and expert competency in a C++ programmer,
and some of the paths by which one can get there. (By the way, even
though I'm hosting the roundtable, you still have to pay for your own
lunch.)
Best regards,
Steve
This week I'm going to let others do my writing for me, with a couple of
pointers to other articles:
...and people accuse me of being retro!
==========================
First off, I came across a hilarious article/blog/press release/whatever
about the one-day design pattern tutorial I gave at Software Development
West in Santa Clara on March 24th. The title of the piece is "Design
Patterns for the People," and rest of the article only gets better!
Read it at http://www.sdmagazine.com/documents/sdmsdw1d/
What is everyone so exercised about?
=========================
Another favorable review of C++ Gotchas will appear in the May issue of
CMP's Windows Developer Network. However, like many others, the author
of the review takes time to comment on Gotcha #7's use of an
unstructured switch statement. (The May issue is available at
http://www.windevnet.com/wdn/issues/ ) See my response to all such
commentary in last week's "Once, Weakly" at
http://www.semantics.org/once_weakly/w11_judgement.pdf
Apologies in order
============
Speaking of "Once, Weakly," I received a number of polite but firm
suggestions that I be more scrupulous about including a links to the
appropriate web sites to obtain referenced documents. Consider it done!
Steve
Main: http://www.semantics.org
C++ Gotchas: http://www.semantics.org/cpp_gotchas/index.html
Once, Weakly: http://www.semantics.org/weekly.html
Code: http://www.semantics.org/code.html
Publications: http://www.semantics.org/publications.html
The latest installment of "Once, Weakly" takes to task all those who
have criticized Gotcha #7: "Ignorance of Base Language Subtleties" from
C++ Gotchas (Addison-Wesley, 2003). As I defend myself against the
forces of zealotry, I also find time to examine non-standard switches to
implement coroutines and opine that playing with dolls and coding in C++
do not mix.
Steve
This week's "Once, Weakly" gets back to basics with an introduction to
the "traits" technique for programming with templates. In this
installment, we go over traits basics, and show how traits can be used
to extend a coding convention to types that do not support the
convention. Future installments will show how traits can be used in
more novel ways. See http://www.semantics.org/weekly.html
Also, a reminder that I'll be giving a tutorial and three talks at SD
2003 West in Santa Clara next week. Descriptions are available at
http://www.cmpevents.com/SDw/a.asp?option=G&V=3&id=91947
Best regards,
Steve
In our latest "Once, Weakly" we look at one of the more obsure syntactic
problems of template programming: how to guide the compiler's parse
when there is not enough information available for the compiler to do
the job on its own. Along the way we discuss and disambiguate the
"rebind" mechanism employed by the standard containers.
Steve
The current "Once, Weakly" is a discussion of why one might want to
implement a clone operation in a hierarchy:
Attack of the Clones:
This "tutorial" item looks at the utility of implementing a
clone operation on a hierarchy, and discusses its implications
for designing frameworks and ordering a meal in a Swedish
restaurant.
On another topic, I'll be speaking at Software Development West in Santa
Clara, CA the week of March 24th. I'll be giving the following
presentations:
1-Day Tutorial: Practical Design Patterns in C++
Writing and Augmenting STL Generic Algorithms
C++ Hierarchy Design Idioms
C++ Gotchas
I also understand that Addison-Wesley will be having me do at least one
book signing for "C++ Gotchas."
Hope to see you there!
Steve
Our new "Once, Weakly" examines some of the tradeoffs of designing an
"instantiation interface" for a class template. This is the interface
that a user of the class template must negotiate in order to produce a
working type from the template. As with any abstract interface, an
effective instantiation interface must balance the opposing forces of
flexibility and ease of use. In this case, we look at tradeoffs between
the uses of typename template parameters and template template
parameters.
See http://www.semantics.org/weekly.html
Steve
This time, "Once, Weakly" gets systematic. We've gathered together most
of our compile time type manipulation techniques to make a start at a
unified framework for the manipulation of types at compile time.
We discuss design of a framework for the dismantling of an arbitrary
type into its component parts, and its later regeneration for the
purpose of compile time type manipulation. We also show how to extend
the framework to include arbitrarily fine detail on type-related
information. We then show how some simple pattern matching and
substitution and replacement can be performed on a dismantled type
before a normal type is regenerated from it.
In effect, we now have two equivalent, but structurally distinct,
versions of the same type. The normal version is optimized for use in a
traditional fashion: accessing its operations, causing code to be
generated that will execute at runtime, etc. The dismantled version is
optimized for compile time analysis and manipulation. Therefore, we now
have the ability to move an arbitrary type between representations
according to how we want to use it. The invertibility of the
representations assures us that either representation will contain all
the information present in the other.
See http://www.semantics.org/weekly.html
The source code for the framework is available at
http://www.semantics.org/code.html
Best regards,
Steve
Once, Weakly: Template Dismantling
==========================
It's not uncommon in template metaprogramming to dismantle a type into
its component parts and put it back together differently. In this
episode, we'll do the same thing with types that are generated from
templates. We'll first show how to determine whether a given type was
generated from a template. Then we'll see how to work back from the
instantiated type to the individual components of its instantiation
(including the instantiated template itself), mix them up, replace them,
tweak them, and produce a new type.
See http://www.semantics.org and follow the "Once, Weakly" link.
Best regards,
Steve
In the latest installment of Once, Weakly we'll discuss the use of
template metaprogramming to improve efficiency while keeping the users
of our facility in happy ignorance of what's going on behind the
scenes. Along the way we'll look at properly general generic
algorithms, automated loop unrolling, algorithm selection based on
whether arguments are integer constant-expressions, and the beneficial
aspects of ignorance.
See http://www.semantics.org and follow the "code" link.
Best regards,
Steve
I've made some new code available in the "code" section of
semantics.org:
====== TYPEINTS ======
Have you ever had to perform compile time arithmetic on 1000-bit
integers? It happens. Unfortunately, C++ compilers generally limit the
precision of integer constant-expressions to 32 or 64 bits. A typeint
is a way of representing an extended precision integer as a type, and
arithmetic calculation as type algebraic manipulation.
The typeint implementation given here will be described in more detail
in my February C/C++ Users Journal online experts column. In addition
to the typeint implementation, there is a reimplementation of the typeof
operator to use typeints for its extended precision compile time
arithmetic.
====== NAMED EXPANDING MONOSTATES ======
The code for the Named Expanding Monostate protopattern described in the
Once, Weakly of 6 November is now available as well. An Expanding
Monostate is a version of the Monostate pattern that allows additional
data members to be added or removed from the Monostate at compile time
without having to modify the Monostate code. A Named Expanding
Monostate allows the use of readable names for the Monostate members.
The most recent "Once, Weakly" examines the use of "type-structures" as
a compile time analog of runtime data structures. We discuss how to
construct and manipulate at compile time simple lists of integers,
function pointers, types, and templates. Follow the "Once, Weakly" link
at http://www.semantics.org.
Best regards,
Steve
The latest Once, Weakly concerns SFINAE (substitution failure is not an
error). This fairly straightforward language feature, whose intended
use is to facilitate function template argument deduction, has some
rather surprisingly effective applications in template metaprogramming.
Best regards,
Steve
COMPLETE TYPEOF IMPLEMENTATION AVAILABLE
---------------------------------------------------------------
With the publication of the December C/C++ Users Journal containing part
3 of the typeof implementation, I am able to release the code in its
entirety. You'll find it at http://www.semantics.org/code.html
The code differs somewhat from that which appeared in the article
series. The major changes are simply some factoring for simplification
and (some) improvement in compilation speed. This code stresses the
limits of class template partial specialization, and the only compiler
that I have personally verified to translate it completely is Comeau
4.3.0. Metrowerks 7.0 does quite a reasonable job, but has some trouble
with qualified pointer to class member types and pointer to function
member types. I hope to provide an appropriately limited version of the
code for Metrowerks soon, and (if I ever get around to installing it) I
suspect the Microsoft Visual C++.net beta (aka version 7.1) will handle
it as well.
C++ GOTCHAS CODE AVAILABLE
-----------------------------------------
Also, the code examples for the new "C++ Gotchas" book are available.
See http://www.semantics.org/cpp_gotchas/index.html
The accompanying book will be available by the end of the month,
according to amazon.com, or by early December, according to
Addison-Wesley.
Best regards,
Steve
After a long hiatus, the Once, Weakly feature is coming back to life!
The first installment in our new series discusses the use of a template
member function in a non-template class to implement an extended version
of the Monostate pattern.
Additionally, we've added a satellite web site for C++ Gotchas
(Addison-Wesley, 2003) that should be available in early December. The
site is not fully live yet, since the book's code examples are not yet
online, but it might be worth a look. The code examples *should* be
there within a week.
A sad side effect of the book's appearance will be the removal of the
existing "Once, Weakly" material from the site, since that has been
incorporated in (one hopes) improved form into C++ Gotchas.
Best regards,
Steve
TCS3
--------------------
Reminder: The "early bird" registration period for THE C++ Seminar will
end Sunday, September 15th. If you plan to attend the seminar, please
register before the deadline (or by Monday at the latest) in order to
receive a 20% discount. The 20% discount is also available after
September 15th to groups of 4 or more individuals from the same
organization. See http://www.thecppseminar.com for details.
There have been some changes and additions to the draft schedule of TCS
talks. See http://www.thecppseminar.com/03/index.htm for details of
the augmented schedule.
Godel Numbering
--------------------
The code that accompanies my October C/C++ Users Journal "Common
Knowledge" column, "A Bit-Wise typeof Operator, Part 2" is available at
http://www.semantics.org/code.html. This is the portion of the typeof
implementation that handles multiple precision integral shifts at
compile time, and compile time translation of a C++ type into a unique,
structured integer. The complete typeof implementation will be
available when the last installment of the column is published in
December.
Best regards,
Steve
TCS3
====
First, a somewhat delayed reminder that The C++ Seminar will be held in
Vancouver, WA (near Portland, OR) October 28-30. We have a draft
schedule of talks on display at
http://www.thecppseminar.com/03/index.htm#schedule
I'll be giving three talks:
What’s int Got To Do With It?
-------------------------------
Template metaprogramming is heaps of fun, but sometimes limitations on
the precision of compile time arithmetic gets in the way of complete
enjoyment. What if you need to perform compile-time arithmetic on
integers that occupy hundreds or thousands of bits, rather than the 32
or 64 that are typically available? This talk discusses a couple of ways
to get around these limitations. Using a fleshed-out implementation of
a portable typeof operator as motivation, we first solve the problem of
performing arithmetic on integers of unbounded length at compile time.
We then consider the approach of abandoning compile time arithmetic, per
se, in preference to performing arithmetic-like operations on types.
Often, the actual results of the operations are not of interest as long
as abstract arithmetic operations can be performed on the types. Other
times, we can manipulate integers of unbounded magnitude as types and
convert (portions of) the result to integers for more conventional use.
Along the way, we’ll discuss how compiler limitations of various kinds
affect how we code these approaches.
Do You Copy?
---------------
The semantics of copying in C++ are subtle, complex, and important. The
reasons behind this complexity are not frivolous. When we design an
abstract data type, we extend the base C++ language with a new data type
that is integrated into the rest of the type system. On one hand, we are
engaged in programming language design in order to produce a usable,
integrated type. On the other hand, we are engaged in translator design,
in that we must convince the compiler to translate our implementation of
the abstract data type efficiently. The details of copying class objects
are essential to efficient, production quality use of data abstraction.
Equally important to efficiency, of course, is correctness.
Unfamiliarity with the necessarily complex semantics of initialization
in C++ can lead to misuse. In this talk we’ll examine issues of
implementing copy operations, as well as how to convince the compiler to
optimize user-defined copy operations. We’ll also examine several common
(and several obscure) gotchas associated with misunderstanding the
semantics of copying.
Simply Annoying Template Programming Gotchas
--------------------------------------------------
Steve has been spending a lot of time lately using templates to break
C++ compilers. The compilers have fought back, however, tripping him up
with a variety of nasty inadvertent language extensions, translation
limitations, and perfectly legal but unwanted syntactic and semantic
interpretations. This talk presents several gotchas that, although
simple to describe and repair, have nevertheless cost many days of
frustration and despair.
Early bird registration at a 20% discount is available until 15
September. See http://www.thecppseminar.com/03/registration.htm
A BOOK
=======
Addison Wesley and I have just about finished with production of "C++
Gotchas," which will be available in early December. For a preview of
the cover and contents, see http://www.semantics.org/cppgotchas.html
and
http://www.aw.com/catalog/academic/product/1,4096,0321125185,00.html
A BUG!
======
There was a bug in my August C/C++ Users Journal column!!! See
http://www.semantics.org/code.html
Best regards,
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.thecppseminar.com/
This week's gotcha discusses type codes and how to just say no. Also,
under the relatively new "Code" section, an application of the Typed
Message pattern that employs the Curiously Recurring Template pattern in
its implementation.
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
I've been getting a steady stream of complaints recently about how the
space limitations of the "Common Knowledge" column in C/C++ Users
Journal doesn't permit me to include the full source code for the
facilities described. Also, the print medium isn't conducive to cut and
paste, except in the physical sense.
From now on I'll make the relevant source code available on
http://www.semantics.org/code.html. So far, I've posted source for the
MultiOut, MultiIn, and List facilities that were described in recent
columns.
Best regards,
Steve
This week (after an admittedly long hiatus--don't ask) we'll be looking
at the mechanics of using exceptions. After complaining about
programmers who throw objects of built-in type, we'll look at the
design of effective exception types. In our second gotcha, we'll
examine the proper mechanics for throwing and catching exceptions.
To see this week's gotchas, go to http://www.semantics.org and follow
the "Once, Weakly" link.
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
Copy operations--copy constructors and copy assignment operators--are
essential considerations for every abstract data type. But what happens
when they're virtual or templated? Find out with this week's gotchas.
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
This week we examine the subjects of overriding and the implementation
of virtual functions in C++, and how these features are affected by
multiple inheritance.
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
ITEM THE FIRST:
This week's gotchas deal with what are often considered elementary
design issues: use of accessor functions and choosing the types of data
members. However, mishandling these issues can often have a serious
effect on a design.
ITEM THE SECOND:
I'll be speaking at Software Development West in San Jose in April. My
talks are
C++ Hierarchy Design Idioms
Function Objects and Function Pointers
Operator Overloading for Experts
C++ Gotchas
Writing STL Generic Algorithms
I'm told by the conference organizers that there is a discount
available. I quote:
"We have a special offer for any attendees whom you refer to
SD West 2002. If they use the code 2WSPKR, they will receive
$300 off any multi-day pass with no expiration date! You can
direct them to www.sdexpo.com for more conference and
registration information."
ITEM THE THIRD:
Speaking of discounts, time is running out for the 20% early bird
discount for The C++ Seminar. The early bird discount ends on 31
January. See http://www.gotw.ca/cpp_seminar/02/index.htm
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
Our gotchas this week deal with temporary generation and references.
http://www.semantics.org/weekly.html#thisweeksmaterial
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
THE C++ SEMINAR:
We (finally) have a published talk and panel schedule for The C++
Seminar (Boston, March 18-20). I'll be talking about
- Simple Template Metaprogramming: Techniques for flexibility and
efficiency
- C++ Nightmares: There's something moving under my translation
unit
- C++ Gotchas
This last talk is being co-presented with Herb Sutter. I don't know yet
whether we're going to be in agreement or at each other's throats.
Should be interesting.
The complete schedule is available at http://www.gotw.ca/cpp_seminar/02.
GOTCHA:
Speaking of gotchas, this week's gotcha follows onto last week's
treatment of direct vs. copy initialization with a discussion of the
return value optimizations, both RVO and NRV (for those who like
acronyms).
PROVIDENCE:
It seems you can go home again. I've been invited to give a guest
lecture to the CS 32 (Software Engineering) course at Brown University
(my alma mater) in Providence Rhode Island. That's pronounced "Roe
Diland," by the way. Providence is probably the only place in the
country where one can actually drink a cabinet without getting
indigestion. (A "cabinet" is the local term for what others might call
a "milk shake" or a "frappe.") I'll be talking about "Design Patterns
and C++ Hierarchy Design Idioms" on February 26th.
Steve
What better way to ring in the new year than with a discussion of direct
vs. copy initialization! (Yes, this does imply some rather unflattering
things about the general level of excitement in my social life.)
This week we discuss the difference between direct and copy
initialization, common program transformations that effectively turn
copy into direct initialization, and common errors that arise from
application of the transformation.
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
This week, we have an eclectic set of gotchas, treating problems with
lexical analysis, terminology, and one of the few bugs caused by the
formatting of a declaration.
http://www.semantics.org/weekly.html#thisweeksmaterial
Steve
--
Stephen C. Dewhurst Semantics Consulting, Inc.
508-866-9400 phone PO Box 997
508-866-9401 fax Carver, MA 02330
scd@...http://www.semantics.org
C++/Object-Oriented/Design Pattern
Consulting, Training, and Mentoring
==================================================
Check out THE C++ Seminar: 3 Days with 5 Experts
http://www.gotw.ca/cpp_seminar/
"THE C++ Seminar" (http://www.gotw.ca/cpp_seminar/) featuring Scott
Meyers, Herb Sutter, Dan Saks, Andrei Alexandrescu and yours truly is
now open for general registration. This time we're holding the seminar
in my neck of the woods, Boston Massachusetts, March 18-20, with an
all-new slate of talks and panels.
Hope to see you there!
Thanks,
Steve