Topic: more general template parameters/uniqueness of temporaries
Author: dak@messua.informatik.rwth-aachen.de (David Kastrup)
Date: 21 Feb 93 19:37:43 GMT Raw View
Template parameters are restricted. It remains to be discussed
what relaxations are to be wished.
My special case: I was doing a template class for modular arithmetic.
Now I want to have the possibility of having a constant value for
the modulus, but a variable value is needed sometimes as well.
Using a static member to indicate the modulus is inflexible, because
one might need several. In addition, constant moduli might be more
efficient. So templates are the way to go.
My dream candidate would have been template <const unsigned &modulus>.
This would have allowed the constant version, as well as the
version with variable modulus. For this example to work as efficiently
as possible, the following language rules would ideally have to hold:
const references are allowed as template arguments.
They have to refer either to an object
with external linkage, or have to evaluate to a constant expression.
Template instantiations with the same constant expression share
the same code.
Note that this is debatable, because with language rules, the
compiler has to create (in principle) an initialized temporary
for the constant expression in debate. In order to allow
this feature, the following must hold for consistency:
The compiler might choose to use the same temporary whenever
a const& is initialized with the same constant expression.
That is, const& variables referring to a constant expression
are not guaranteed to point to unique temporaries. Currently
I can see no definite statement to that matter in the ARM.
This is important, because external names can be mangled, and
constants can be mangled, but local temporaries hardly can.
Of course you can achieve the same effect with a template argument
of <const unsigned *>. But then the syntax is a pointer
syntax everywhere, which I find less readable.
Any opinions?
What other template arguments would people wish to have?
--
David Kastrup dak@messua.informatik.rwth-aachen.de
Tel: +49-241-72419 Fax: +49-241-79502
Goethestr. 20, W-5100 Aachen, Germany