Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2002-02-19 15:21:13


Late one night in Copenhagen, after a few beers with Andy Sawyer and
Dietmar, I proposed the generic-by-default extension, though I was calling
it "implicit templates". I hadn't gone so far as to think about making
type names optional. Glad to hear other share my pain in typing "template
blah blah" :)

BTW, the whole optional type name thing is a characteristic of some
statically typed functional languages like ML. However, the type system of
ML has to be a lot tighter (fewer chances for ambiguity) than C++ to allow
for the type inferencing. For example, overloading is severally restricted
in ML, which I think is bad because overloading is important for generic
programming. So, in short, I don't think type inference is really a viable
option for C++.

So going back to the implicit template idea, but with type names, we get
the following problem. I assume the implicit templates work like this: a
type in a function decl is really a type parameters if the type is not
previously defined. This is fragile. What happens if the program is later
changed so that the type name is previously defined? Badness, your
function template silently turns into a normal function.

program version 1:
  // an implicit function template
  T foo(T x) { return x + x; }

program version 2:
  typedef int T;
  ...
  // no longer a function template :(
  T foo(T x) { return x + x; }

So my personal nirvana for function templates would instead use the
following syntax (this is a joke)

<T> foo(<T> x) { return x + x; }

learning-to-type-fasterly-yours,
Jeremy

On Tue, 19 Feb 2002, Greg Colvin wrote:
gcolvi> And to expand on my cryptic suggestion, I am proposing that C++
gcolvi> functions be generic by default, with type names optional, and
gcolvi> needed only when they cannot be deduced. Freedom! Concision!
gcolvi> Long live the Spirit of C!
gcolvi>
gcolvi> For a precedent, I think this syntax is not unlike ALGOL's call-
gcolvi> by-name.
gcolvi>
gcolvi> For total bliss we can add unnamed functions, e.g.:
gcolvi>
gcolvi> f = (t,s) { return t+s; }
gcolvi>
gcolvi> Or much less generically:
gcolvi>
gcolvi> int (*f)(int,int) = (int t,int s) { return t+s; }
gcolvi>
gcolvi> And for Nirvana, we can make C++ completely expression oriented,
gcolvi> like ALGOL. Yes, I am getting out on the edge, but I'm serious.

----------------------------------------------------------------------
 Jeremy Siek http://php.indiana.edu/~jsiek/
 Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
 C++ Booster (http://www.boost.org) office phone: (812) 855-3608
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk