Source Code
This page contains C++
code from a variety of sources:
- Complete source for my "Common Knowledge" column that appears in The C/C++ Users Journal or in CUJ's
"Online Experts" forum.
- Source that accompanied this site's (currently inactive) "Once,
Weakly" web column.
- Stuff I've been thinking about.
NOTE:
Most of the code below
has now been incorporated in a more systematic way into the Tyr
library.
If,
on the other hand, you're
looking for the source code for C++
Gotchas,
you'll find it here.
Each source file should
contain a copyright notice similar to the
following:
//
// Copyright (c) 2003 by Stephen C. Dewhurst
// Permission to use, copy, modify, distribute and sell this software for any purpose is
// hereby granted without fee, provided that the above copyright notice appears in all copies
// and that both that copyright notice and this permission notice appear in supporting documentation.
// The author makes no representations about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//
Some warnings:
First, these facilities were developed primarily
to
illustrate C++ programming techniques, and are not necessarily
generally
useful (some are even kind of weird). Second, a lot of the code
employs
template features that are not handled well by some C++
compilers. If you have problems with some of the template code,
please check it first with a highly standard compliant compiler before
making a bug report.
Each set of files is
fairly self-contained, and should not require
anything more than the C++ standard library and a main function in
order to
run. If a set of files contains a main.cpp file, then you should
be able to produce an executable by copying the files to a directory
and doing a CC *.cpp (or
the local equivalent).
Brief Index
- Compile-time type manipulation
- typeof operator implementations
- STL iterators
- STL containers
- Extended precision compile-time arithmetic
- Patterns
Type
Dismantling
A framework for the
dismantling, pattern matching/substitution, and
regeneration
of a type. See a
description
of the framework in
the Once, Weakly section of this site.
dismantle.h
Typelist Meta-Algorithms
Algorithms, meta-function
objects, and meta-function object adapters
for manipulating typelists. See a decription of the techniques
in
the Once, Weakly section of this site.
This code has been
updated, as described in another Once, Weakly.
typelist.h
utils.h
metafunction.h
typlistalgorithms.h
main.cpp
Named
Expanding Monostate protopattern
This is a technique in
search of an application. See a
description of the technique in the Once, Weakly section
of this
site.
expandingmonostate.cpp
Typeints
CUJ 21(6), June 2003: "N-Ary
Nibbling"
Moving typeints from a
binary representation to a 2**n representation
in order to deal with compilers' restrictive template instantiation
recursion
depth limits. Compile time arithmetic in excess of 5000-bit
precision is possible.
typeint_nary.h
utils.h
main.cpp
CUJ
21(4), April 2003: "Scouting
Out an Optimization"
A detailed examination of a typeint multiplication algorithm, and
discussion
of metaprogramming techniques for improving its compile time.
typeint_mul.h
utils.h
main.cpp
CUJ
21(2), February 2003: "Typeints"
This facility is the first version of a library that performs
extended-precision
compile time arithmetic using the type system instead of binary
arithmetic.
Depending on the capabilities of the compiler, it's possible to perform
compile time arithmetic on 1000-bit (or larger) integers.
The first set of files
consists of a simple version of the typeint
facility
and a test program.
typeint.h
utils.h
main.cpp
The
second set of files is a
reimplementation
of the typeof operator implementation (see below) using typeints to
perform
the extended-precision compile time arithmetic.
gengn.h
gentype.h
typeof.h
typeint.h
utils.h
Typeof
CUJ 20(8), August 2002: "A Bit-Wise Typeof
Operator, Part
1"
CUJ 20(10), October 2002: "A Bit-Wise Typeof
Operator,
Part 2"
CUJ 20(12), December 2002: "A Bit-Wise Typeof
Operator,
Part 3"
Here is the source code
for the of the typeof implementation that
translates
a type into a Gödel number, then back to a type in order to
implement
most
of the functionality of a built-in typeof operator. It includes
some
elementary type manipulation utilities, the multi-integer compile time
shift operations, the Gödel number generator, and the Gödel
number to type regeneration. The code is almost
identical to that which appeared in the article, but has been factored
somwhat to make it cleaner, and (secondarily) make it compile a little
faster.
utils.h
multishift.h
gengn.h
gentype.h
typeof.h
CUJ 20(8), August 2002:
"A Bit-Wise Typeof Operator,
Part
1"
There was a bug in one of the code examples. I had this
to say about it in the December, 2002 CUJ.
List
CUJ 20(6), June 2002: "Running Circles Round
You,
Logically":
List
List (great name, huh?)
is an STL-like container that uses
two-way pointers to implement a singly-linked list that can change its
mind as to which way it's organized. Its iterators are similarly
confused, and this makes for some interesting complexity results.
Note: A problem
with the use of ptrdiff_t to hold a pointer value
has been fixed. Use list2.h in preference to the original list.h.
Note 2: See flist, below, for a better altenative to List.
list2.h
list.h
flist
flist is an STL-compliant version of List. flist uses 2-way
pointers to implement a container that has the same members and
performance as std::list, but that uses half the working storage.
It also provides fickle-bidirectional iterators and a constant time
reverse operation.
flist.h
flist documentation
MultiIn
CUJ 20(4), April 2002: "Metaprogrammed
Adapters": MultiIn
MultiIn is an
input/forward iterator adapter that allows an
unbounded number of differently-typed input/forward sequences to be
treated
as a single "supersequence," without copying the values from the
constituent
sequences.
multiin.h
MultiOut
CUJ 20(2), February 2002: "Output Iterator
Adapters": MultiOut
MultiOut is an output
iterator adapter that allows an output
sequence to be sent to an unbounded number of output streams
simultaneously.
multiout.h