Topic: template objects


Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 7 Nov 1994 04:12:59 GMT
Raw View
In article <38v32n$sc8@st6000.sct.edu> gcato@sct.edu (Gene Cato) writes:
>
>obj<int> is the name of a class if obj is the name of a class template.
>As I understand it, under the current draft, obj<int> could also be an
>instance of a template function if obj is a function template as in
>
>template <class T> void f(T &p); //#1
>
>In this case f<int> would refer to the the function template f with the
>template parameters <int>.

Although in this case 'f' is enough to identify the function template,
in general function templates can be overloaded and the full
formal and function signature must be stated to identify it.

This means to identify a template function (instance) you must
give (in effect) the whole declaration of the template of which it is an
instance as well as the parameter bindings (or function signature).

That is, in general

 f<int>

is NOT the unambiguous name of an instance of a template:

 template<class T> int f(T*);

is another template overloading the first and _its_ f<int>
is quite distinct:

 f<int>(int&);
 f<int>(int*);

are two different functions. Its worse than this though, even
_that_ information is not enough, as I said above:

 template<class T> void g(int, T);
 template<class T> void g(T, int);

 g<int>(int,int) // which function?

The current 'understanding' is that a call

 g(1,1); //#1

is ambiguous, but that is NOT always the case:

 // file 1
 template<class T> void g(int, T);
 g(1,1);

 // file 2
 template<class T> void g(T, int);
 g(1,1);

There is no ambiguity here, and there is no breach of the
One Definition Rule because the two instances are DISTINCT functions.

At least I think this is the status quo: it is hard to be sure,
the WP is not clear.  I deduce this is the case because people
tell me the call #1 above is ambiguous: if there were only
ONE function g(int,int) there would not be any ambiguity
during overload resolution (but there might be a breach of
the ODR due to multiple definitions).

I've suggested a different model: the
set of overloaded function templates simply declares a single
(unified) family of overloaded functions. In that case
only the function signatures are required to identify a template
function.

The committee has to make up its mind which model is intended:
are templates generic functions or boilerplates for
creating ordinary functions?

Because the two models have distinct consequences.
In particular, the mangled name of a template
function in the unified model (my suggestion) would be

 g(int,int) // ordinary function signature!

and in the current model must be equivalent to

 g($1,int)<int> // $1 means first parameter

[Note in the unified model the form

 dynamic_Cast<Derived*>(Base*)

is used if there are NONDEDUCIBLE parameters: these are families
of function with the same signature with names qualified
by the non-deducible arguments: this has nothing to
do with the overloading problem]

In the unified model the call

 template<class T> void g(int, T);
 template<class T> void g(T, int);
 g(1,1);

is NOT ambiguous: it calls g(int,int).

--
        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: pete@genghis.interbase.borland.com (Pete Becker)
Date: Fri, 28 Oct 1994 12:50:03 GMT
Raw View
In article <38pjag$1p9h@st6000.sct.edu>, Gene Cato <gcato@sct.edu> wrote:
>It seems to me that a logical extension to the current ability to
>explicitly specify a specific template function by specifying it's template
>parameters, that you could just as easily specify specific template objects.
>For example
>
>template <class T> T obj;
>
>and then refer to obj for a particular type as
>
>{
>  obj<int> = 5;
>}
>
>I am not sure this would have much practical use.  But the syntax seems right.
>

 obj<int> is the name of a class. You can use it in any context where
you can use any other class name. In most cases, you use it to create objects
of that type:

 obj<int> foo; // create an object named 'foo' of type obj<int>

 -- Pete






Author: gcato@sct.edu (Gene Cato)
Date: 27 Oct 1994 21:14:56 -0400
Raw View
It seems to me that a logical extension to the current ability to
explicitly specify a specific template function by specifying it's template
parameters, that you could just as easily specify specific template objects.
For example

template <class T> T obj;

and then refer to obj for a particular type as

{
  obj<int> = 5;
}

I am not sure this would have much practical use.  But the syntax seems right.

  Until Later,
  Vaughn Cato (gcato@st6000.sct.edu)




Author: gcato@sct.edu (Gene Cato)
Date: 29 Oct 1994 23:14:31 -0400
Raw View
In article <CyDvnF.1F4@borland.com> pete@genghis.interbase.borland.com
(Pete Becker) writes:
>
>In article <38pjag$1p9h@st6000.sct.edu>, Gene Cato <gcato@sct.edu> wrote:
>>It seems to me that a logical extension to the current ability to
>>explicitly specify a specific template function by specifying it's template
>>parameters, that you could just as easily specify specific template objects.
>>For example
>>
>>template <class T> T obj;
>>
>>and then refer to obj for a particular type as
>>
>>{
>>  obj<int> = 5;
>>}
>>
>>I am not sure this would have much practical use.  But the syntax seems
>>right.
>
> obj<int> is the name of a class. You can use it in any context where
>you can use any other class name. In most cases, you use it to create objects
>of that type:
>
> obj<int> foo; // create an object named 'foo' of type obj<int>
>
> -- Pete
>
>

obj<int> is the name of a class if obj is the name of a class template.
As I understand it, under the current draft, obj<int> could also be an
instance of a template function if obj is a function template as in

template <class T> void f(T &p);

In this case f<int> would refer to the the function template f with the
template parameters <int>.

This allows you to write

{
  int x;
  f<int>(x);
}

in addition to the original way of calling a template function

{
  int x;
  f(x);
}

where the template parameters are deduced from the type of the actual
parameters to the function.

Given that f<int> is a valid way to specify a template function that takes
the parameters <int>, it seems that a natural consequence would be the ability
to specify template objects as in the above example with obj.

  Until Later,
  Vaughn Cato (gcato@st6000.sct.edu)




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 30 Oct 1994 14:50:47 GMT
Raw View
gcato@sct.edu (Gene Cato) writes:

>It seems to me that a logical extension to the current ability to
>explicitly specify a specific template function by specifying it's template
>parameters, that you could just as easily specify specific template objects.

Yes, allowing template classes and template functions but not template
variables is just YAGNO (Yet Another Gratuitous Non-Orthogonality ;-).

John Skaller recently put a proposal to the ANSI/ISO C++ committee to remove
this restriction.

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




Author: bs@research.att.com (Bjarne Stroustrup <9758-26353> 0112760)
Date: Sun, 30 Oct 1994 21:53:28 GMT
Raw View

fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:

 > gcato@sct.edu (Gene Cato) writes:
 >
 > >It seems to me that a logical extension to the current ability to
 > >explicitly specify a specific template function by specifying it's template
 > >parameters, that you could just as easily specify specific template objects.
 >
 > Yes, allowing template classes and template functions but not template
 > variables is just YAGNO (Yet Another Gratuitous Non-Orthogonality ;-).
 >
 > John Skaller recently put a proposal to the ANSI/ISO C++ committee to remove
 > this restriction.

One man's YAGNO is another man's YAGE (yet another gratuitous extension :-).

Please remember that almost everything is being proposed, suggested, demanded,
etc. Also, very few people have an accurate idea of how many ideas have already
been considered. Please also note that almost nothing is going to be added to
C++ at this late stage. Most of the efforts of the committee are focussed on
producing a comprehensive and clear draft standard.

 - Bjarne

PS My observation is general, and not directed against any particular suggestion.




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 31 Oct 1994 17:06:09 GMT
Raw View
In article <38pjag$1p9h@st6000.sct.edu> gcato@sct.edu (Gene Cato) writes:
>It seems to me that a logical extension to the current ability to
>explicitly specify a specific template function by specifying it's template
>parameters, that you could just as easily specify specific template objects.
>For example
>
>template <class T> T obj;
>
>and then refer to obj for a particular type as
>
>{
>  obj<int> = 5;
>}
>
>I am not sure this would have much practical use.  But the syntax seems right.

 More precisely:

 // header file
 template<class T> extern T const pi;

 // definition file
 template<class T> T pi=1.414;

 // use
 #include "header"
 .... pi<double> * pi<double> ...

 template<class T> sin(T t) { .... pi<T> ... }

It could be very useful. You can, of course, already do it!
Like this:

 template<class T> class math {
  static T pi;
 }
 ...
 template<class T> T math<T>::pi = 1.414;

 .... math<double>::pi ...
 .... math<T>::pi ....

which proves that the semantics are sound.

Similarly, you can ALREADY have templated enumerations
and templated typedefs for the simple reason you can
put these things inside classes, and you can templatise classes.

--
        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: gcato@sct.edu (Gene Cato)
Date: 31 Oct 1994 19:08:39 -0500
Raw View
In article <38u2io$bsc@engnews2.eng.sun.com> clamage@Eng.Sun.COM
(Steve Clamage) writes:
>>{
>>  obj<int> = 5;
>>}
>
>How is this different from saying
> int obj = 5;

saying obj<int> would be referring to the obj template object that is
instantiated with the template parameters <int>.  It would not be declaring
a new variable called obj.  obj<int> would be referring to a specific
instance of an object when obj is an object template in the same way that
List<int> refers to a specific instance of a class when List is a class
template, or f<int> refers to a specific function when f is a function
template.

  Until Later,
  Vaughn Cato (gcato@st6000.sct.edu)




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 29 Oct 1994 17:59:52 GMT
Raw View
gcato@sct.edu (Gene Cato) writes:

>It seems to me that a logical extension to the current ability to
>explicitly specify a specific template function by specifying it's template
>parameters, that you could just as easily specify specific template objects.
>For example

>template <class T> T obj;

>and then refer to obj for a particular type as

>{
>  obj<int> = 5;
>}

How is this different from saying
 int obj = 5;


--
Steve Clamage, stephen.clamage@eng.sun.com