[Dev] Fun with templates

Tanner Lovelace dev@trilug.org
13 Feb 2002 15:43:32 -0500


--=-MZxUXfw7MP/vws/ywymO
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Good explanation, Brent.  I think you got everything that
was there. :-)

Ed Hill asked:

>Ok, so you can do calculations of arbitrary complexity at compile time
>with templates.  Cool.  But I've seen only a few practical examples of
>anything useful accomplished with these template gymnastics.  The
>applications that I'm aware of are expression templates:
>
>  http://www.acl.lanl.gov/pete/index.html
>  http://www.acl.lanl.gov/pooma/
>  http://www.oonumerics.org/blitz/
>
>and I've used them a little.
>
>So whats my point, then?  ;-)  Well, do any of you actually use ETs or
>related template methods for your projects?  If so, do you have any
>examples you'd be willing to share?  I'd be interested in seeing them!

The Blitz++ library is probably one of the best examples of this
I've seen.  They basically use templates and application domain
knowledge to control things like loop unrolling, etc..  I've
even seen side by side comparisons of assembly code generated
by their C++ template program and hand coded assembly and
they're about the exact same thing.

As to whether anyone around here actually uses them, my guess
would be not that much.  ETs are a very advanced C++ concept.
Not even the C++ standards committee realized what could
be done with them until someone came in with a program
that calculated primes in the error messages.  I think
it will take a while before more people start using them.
I would expect, however, that the same guys that would
think about doing hand coded assembly might be the first
ones that would try them out, though...

Tanner

On Wed, 2002-02-13 at 13:32, Brent Verner wrote:
> [2002-02-12 16:26] Tanner Lovelace said:
> | All these questions about the STL had me thinking about a class
> | I took a few years ago about advanced templates and the STL.
> | I thought I might share a few things with the list, if anyone
> | is interested.
> |=20
> | First, did you know that apparently templates are turing complete!
> | I.e. you can actually calculate things using templates.  If you
> | don't believe me, save the following program as fibonacci.cpp
> | and run this command:
>=20
> nice.
>=20
> | g++ -c fibonacci.cpp 2>&1 | grep Output
> |=20
> | // -- Cut here ---
> | template <int n, int f> struct Output {};    // creates output
> |=20
> | template <int n> struct fibo {
> |     enum { val =3D fibo<n-1>::val + fibo<n-2>::val };
>=20
> caluclate a value in the fibo series.
>=20
> | };
> | template<> struct fibo<1> { enum { val =3D 1}; };
> | template<> struct fibo<0> { enum { val =3D 0}; };
>=20
> initializes the fibo generation. try with the following.
>   template<> struct fibo<1> { enum { val =3D 2}; };
>   template<> struct fibo<0> { enum { val =3D 1}; };
>=20
> | template <int i> struct Fibo_print {
> |     Fibo_print<i-1> a;
>=20
> template recursion, causing the the calculation above to=20
> generate the fibo value in the line below.
>=20
> |     enum { val =3D fibo<i>::val };
> |     void f() { a.f(); Output<i,val> d =3D val; }
>=20
> illegal conversion/assignment generates the error message.
>=20
> | };
> | template<> struct Fibo_print<1> {=20
> |     enum { val =3D fibo<1>::val };
> |     void f() { Output<1,val> d =3D val; }
> | };
>=20
> stops the recursion.
>=20
> | void foo() {
> |     Fibo_print<17> a;
> |     a.f();
> | }
> | // -- End program here --
>=20
> --=20
> "Develop your talent, man, and leave the world something. Records are=20
> really gifts from people. To think that an artist would love you enough
> to share his music with anyone is a beautiful thing."  -- Duane Allman
> _______________________________________________
> Dev mailing list
> Dev@trilug.org
> http://www.trilug.org/mailman/listinfo/dev
--=20
Tanner Lovelace | lovelace@wayfarer.org | http://wtl.wayfarer.org/
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
GPG Fingerprint =3D A66C 8660 924F 5F8C 71DA  BDD0 CE09 4F8E DE76 39D4
GPG Key can be found at http://wtl.wayfarer.org/lovelace.gpg.asc
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
 Those who are willing to sacrifice essential liberties for a little=20
 order, will lose both and deserve neither.  --  Benjamin Franklin=20

 History teaches that grave threats to liberty often come in times
 of urgency, when constitutional rights seem too extravagant to=20
 endure.  --  Justice Thurgood Marshall, 1989=20

--=-MZxUXfw7MP/vws/ywymO
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQA8as/0zglPjt52OdQRAjaMAKDPM073pd3gcQ9nUCoNU3htQWpm7QCfcA7B
H4SigmOWMU0zJuc8DHVlG8w=
=dbv4
-----END PGP SIGNATURE-----

--=-MZxUXfw7MP/vws/ywymO--