Topic: Featudefault template initializers
Author: cbarghie@watcgl.waterloo.edu (Chris Barghiel)
Date: Mon, 23 Mar 1992 06:16:10 GMT Raw View
To instantiate a template class, one must always specify the complete list
of actual parameters between angle brackets, as in this example:
Array<int> ai;
Array<double> ad;
where the Array class template has been defined as follows:
template <class Type>
class Array {
// the body of the class definition
};
The project I have joined recently consists of a rich set of classes contained
in several hierarchies. None of the classes has been implemented with templates,
so the voluminous code that uses them instantiates classes simply as, say,
Array arr; // with an implicit type of 'int', for example;
The current project requirements, however, make the use of templates a must.
Adding the template feature to the class definitions is evidently
worthwhile. On the other hand, changing all the application code to make
the instantiation type explicit seems rather wasteful; should it even be
necessary?
With this question in mind, I consulted the literature looking for what I
thought was obvious: a way to provide a default template type, something
like this:
template <class Type = int>
class Array { ... };
This way, the old code would not even have to be touched! After all, this
feature would have conformed with what has always been standard for C++
functions: default initializers for function arguments within the function
signature. Although not universally applicable, this feature is judged
appropriate in many cases, so why not implement it for templates?
The rules used for functions would also apply to templates. For example,
given the definition:
template <classT1 = double, int size = 10, classT2 = KnownType>
class SomeClass { ... };
one should be able to instantiate SomeClass in various ways:
SomeClass inst1; // double, 10, KnownType
SomeClass<T,12> inst2; // T, 12, KnownType
SomeClass<double,30,T2> inst3; // no defaults; 'double', although
// unchanged, must be specified;
but not:
SomeClass<12> wrong; // as in: double, 12, KnownType;
// error to be caught by compiler;
Needless to say, I am not aware of any C++ compiler that supports default
template initializers. To my mind, this feature would fit perfectly into
the C++ paradigm. It would often prove to be useful - both when templating
previously 'rigid' classes, and when writing new code - and should not pose
any serious problem to compiler writers.
I am curious to learn about your opinions on this matter. Why is this handy
feature not part of the language...or is it (in some implementations)?
Regards,
Chris Barghiel
--
Cristin Barghiel DC 2134, (519)885-1211 x3389
cbarghie@watcgl.waterloo.edu Dept. of Computer Science
cbarghie@watcgl.uwaterloo.ca University of Waterloo, Canada
Author: cox@cbnewsm.cb.att.com (michael.cox)
Date: Tue, 24 Mar 1992 19:26:54 GMT Raw View
In article <1992Mar23.061610.805@watcgl.waterloo.edu> cbarghie@watcgl.waterloo.edu (Chris Barghiel) writes:
.
.
.
>
>The rules used for functions would also apply to templates. For example,
>given the definition:
>
> template <classT1 = double, int size = 10, classT2 = KnownType>
> class SomeClass { ... };
>
>one should be able to instantiate SomeClass in various ways:
>
> SomeClass inst1; // double, 10, KnownType
> SomeClass<T,12> inst2; // T, 12, KnownType
> SomeClass<double,30,T2> inst3; // no defaults; 'double', although
> // unchanged, must be specified;
>
>but not:
>
> SomeClass<12> wrong; // as in: double, 12, KnownType;
> // error to be caught by compiler;
>
>Needless to say, I am not aware of any C++ compiler that supports default
>template initializers. To my mind, this feature would fit perfectly into
>the C++ paradigm...
I agree and apparently so do a lot of others (I've seen this idea posted
more than once). I think the ANSI C++ committee may have adopted it.
I saw an example in a column in the _C++ Report_ that had templates with
a similar syntax (except for the "class T1 = double" syntax).
... Learn something new every day! I just tried an example in Turbo
C++ for Windows similar to:
template <class T, int size = 10>
class Array
{
// internal stuff deleted
};
Array<double, 15> double_array;
Array<int> int_array;
It compiled!! Is this a bug, a feature, or some forward-looking think
on Borland's part ;-)??
BTW, the template didn't compile when I tried a default argument for
the class argument, i.e. "class T=int". Got:
Nontype template argument must be of scalar type
Expression syntax
--
==========================================================================
Michael H. Cox ARPA: cox@garage.nj.att.com
AT&T Bell Labs UUCP: ihnp4!edsel!cox