Topic: template argument types


Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: Wed, 8 Mar 1995 20:11:46 GMT
Raw View
In article <3jjkv2$pev@f111.iassf.easams.com.au>,
Rohan LENARD <rjl@f111.iassf.easams.com.au> wrote:
>In article <D52rrn.CKH@ucc.su.oz.au>,
>John Max Skaller <maxtal@Physics.usyd.edu.au> wrote:
>>
>> Not really.
>>
>> Templates are a kind of safe macro. Even the most basic
>>Macro Assemblers support iteration and conditionals. So at present,
>>there is a whole class of "generic" things which cannot
>>be templatised. For example, a discriminated union with
>>a variable number of types.
>
>A perfect example is this problem (which is caused by the separation of
>preprocesing from compiling) -
>
>template <class T> const char* TypeNameAsText(void)
>{
>  return #T; // gives "T"
>}
>
>This unfortunately can never work, even though if one writes an explicit
>version it will
>
>const char* TypeNameAsText(void)
>{
>  return #int; // gives "int"
>}
>
>Little things like this *really* annoy me :-(

 A nastier kind of problem is:

 template<class T1, class T2> class A2 {
  T1 t1;
  T2 t2;
  A2(T1 a1, T2 a2) : t1(a1), t2(a2) {}
 };
 template<class T1, class T2, class T3> class A3 {
  T1 t1;
  T2 t2;
  T3 t3;
  A3(T1 a1, T2 a2, T3 a3) : t1(a1), t2(a2), t3(a3) {}
 };
....

 No true blue syntax macro processor should
have any trouble with generating all those templates
from a single macro. That is, the template system
itself should be able to do this.

 In fact, it can, almost, using recursion:

 A2<int, A2<int,int> > a( 1, A2<int,int>(2,3) );

is functionally equivalent, but NOT referentially
transparent, and not the same type as A3<int,int,int>.

Consider also a trick of the kind I first saw Erwin Unruh demonstrate:

 template<class T> class X {
  T::first t1;
  X<T::second> t2;
 };
 // specialisation to terminate recursion
 class X<void >{ };

 struct C {
  typedef int first;
  typedef void second;
 };
 struct B {
  typedef int first;
  typedef C second;
 };
 struct A {
  typedef int first;
  typedef B second;
 };
 X<A> x;

This shows that it is _possible_ to provide an iterative
mechanism using tail recursion. However, the formulation
is too tricky to use in practice. I wonder if a more
regular syntax can be invented which is of equivalent
power -- preferbly one which produces and is driven
by "list-like" arguments rather than recursive ones.


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
Date: 8 Mar 1995 13:20:39 -0000
Raw View
Rohan LENARD (rjl@f111.iassf.easams.com.au) wrote:
: A perfect example is this problem (which is caused by the separation of
: preprocesing from compiling) -

: template <class T> const char* TypeNameAsText(void)
: {
:   return #T; // gives "T"
: }

Well, if you want to make life complex for yourself :

  [e:\temp]type test.cpp && hc -Hldopt=/NOL/BATCH test.cpp && test.exe
  #include <typeinfo.h>

  template<class T> const char * name ( T t )
  {
   return typeid(t).name() ;
  }

  #include <iostream.h>

  class Whooppee {} ;

  int
  main ( int, char ** )
  {
   int i = 0 ;
   long l = 0 ;
   Whooppee w ;
   cout << name(i) << endl ;
   cout << name(l) << endl ;
   cout << name(w) << endl ;
   return 0 ;
  }
  MetaWare High C/C++ Compiler R3.31@1
  (c) Copyright 1987-94, MetaWare Incorporated
  int
  long
  Whooppee

  [e:\temp]




Author: rjl@f111.iassf.easams.com.au (Rohan LENARD)
Date: 8 Mar 1995 17:05:06 +1000
Raw View
In article <D52rrn.CKH@ucc.su.oz.au>,
John Max Skaller <maxtal@Physics.usyd.edu.au> wrote:
>
> Not really.
>
> Templates are a kind of safe macro. Even the most basic
>Macro Assemblers support iteration and conditionals. So at present,
>there is a whole class of "generic" things which cannot
>be templatised. For example, a discriminated union with
>a variable number of types.

A perfect example is this problem (which is caused by the separation of
preprocesing from compiling) -

template <class T> const char* TypeNameAsText(void)
{
  return #T; // gives "T"
}

This unfortunately can never work, even though if one writes an explicit
version it will

const char* TypeNameAsText(void)
{
  return #int; // gives "int"
}

Little things like this *really* annoy me :-(

Regards,
 Rohan
--
----------------------------------------------------------------------------
rjl@iassf.easams.com.au | All quotes can be attributed to my automated quote
Rohan Lenard            | writing tool.  Yours for just $19.95; and if you
+61-2-367-4555          | call now you'll get a free set of steak knives ...




Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: Tue, 7 Mar 1995 14:47:46 GMT
Raw View
In article <9506400.2386@mulga.cs.mu.OZ.AU>,
Fergus Henderson <fjh@munta.cs.mu.OZ.AU> wrote:
>isi@panix.com (Atman Jacob Binstock) writes:
>
>>Has there been any proposal to make an argument list be an acceptable
>>argument for a template?
>
>No, not that I know of.
>
>C++ is already pretty complex; why would the additional complexity
>be worth it?  Generally, using a structure rather than an argument
>list works OK.

 Not really.

 Templates are a kind of safe macro. Even the most basic
Macro Assemblers support iteration and conditionals. So at present,
there is a whole class of "generic" things which cannot
be templatised. For example, a discriminated union with
a variable number of types.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 4 Mar 1995 13:08:46 GMT
Raw View
isi@panix.com (Atman Jacob Binstock) writes:

>Has there been any proposal to make an argument list be an acceptable
>argument for a template?

No, not that I know of.

C++ is already pretty complex; why would the additional complexity
be worth it?  Generally, using a structure rather than an argument
list works OK.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: isi@panix.com (Atman Jacob Binstock)
Date: 23 Feb 1995 18:54:49 -0500
Raw View
Has there been any proposal to make an argument list be an acceptable
argument for a template?
ex:
 template<class T, arglist A> T* heap(A a) { return new T(a); }
or
 template<class R, arglist A> class functor {
 public:
  virtual R operator()(A) = 0;
 ...

The second example would make it necessary to specify particular
arglists, perhaps like:
 class stringcompare : public functor<bool, (char*,char*) > {
 ...

How about a function name as a template argument?
ex:
 template<class T> class delegate {
  T* ptr;
 public:
  template<class R, function F, arglist A> R F(A a)
  { return ptr->F(a); }
 ....

--
Atman Binstock                                       Integrated Software Inc.
isi@panix.com                                        speaking only for myself