Topic: Template metaprogramming in C++0x


Author: pdimov@mmltd.net (Peter Dimov)
Date: Wed, 14 Nov 2001 18:22:41 GMT
Raw View
The idea presented in this post have been sparked by a discussion -
some time ago - between Andrei Alexandrescu and Gabriel Dos Reis about
template template parameters.

Andrei proposed that template template parameters be defined as

template<template T> void f()
{
  T<int> t;
}

making them more flexible.

Gabriel Dos Reis countered that - I'm paraphrasing his arguments but
he'll correct me if I've misinterpreted - this change would make the
compile-time meta-language type system less strict. Which is certainly
true.

But.

What is the difference between statically typed and dynamically typed
languages? The former group detect type errors at compile time,
whereas the latter catch type errors at runtime.

This distinction is, however, meaningless for a compile-time
meta-language! Whether it's statically or dynamically typed, errors
will be caught at compile time.

Therefore, I propose a logical extension to the C++ compile-time
meta-language: make all template parameters typeless.

class nil;

template<Head, Tail> class pair;

template<List> class length;

template<> class length<nil>
{
  enum { value = 0 };
};

template<Head, Tail> class length<pair<Head, Tail> >
{
  enum { value = 1 + length<Tail>::value };
};

int main()
{
  std::cout << length<pair<1, pair<length, pair<int, nil> > > >::value
<< '\n';
}

--
Peter Dimov
Multi Media Ltd.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: kavdeiv@mail.ru (Kiril Avdeiv)
Date: Thu, 15 Nov 2001 11:10:52 GMT
Raw View
pdimov@mmltd.net (Peter Dimov) wrote in message news:<7dc3b1ea.0111140800.5e9fa8a7@posting.google.com>...
> The idea presented in this post have been sparked by a discussion -
> some time ago - between Andrei Alexandrescu and Gabriel Dos Reis about
> template template parameters.
>
> Andrei proposed that template template parameters be defined as
>
> template<template T> void f()
> {
>   T<int> t;
> }
>
> making them more flexible.
>
> Gabriel Dos Reis countered that - I'm paraphrasing his arguments but
> he'll correct me if I've misinterpreted - this change would make the
> compile-time meta-language type system less strict. Which is certainly
> true.
>
> But.
>
> What is the difference between statically typed and dynamically typed
> languages? The former group detect type errors at compile time,
> whereas the latter catch type errors at runtime.
>
> This distinction is, however, meaningless for a compile-time
> meta-language! Whether it's statically or dynamically typed, errors
> will be caught at compile time.
>
> Therefore, I propose a logical extension to the C++ compile-time
> meta-language: make all template parameters typeless.
>
> class nil;
>
> template<Head, Tail> class pair;
>
> template<List> class length;
>
> template<> class length<nil>
> {
>   enum { value = 0 };
> };
>
> template<Head, Tail> class length<pair<Head, Tail> >
> {
>   enum { value = 1 + length<Tail>::value };
> };
>
> int main()
> {
>   std::cout << length<pair<1, pair<length, pair<int, nil> > > >::value
> << '\n';
> }

I loathe this idea for the following good reasons:
1. I and a great deal of other C++ developers have already learned the
langugae the way it is now. And making such a drastic change will only
dump on us a bunch of new problems.
2. I think that it is wise to first deal with the template related
problems in the current definition of the language. Do take a look at
the number of template related DRs. What is the point in suggesting
core language additions to the language that has not yet been polished
and whose definition is still badly afflicted with inconsistencies and
ommisions? It will simply make things worse.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]