Re: Redefining template parameters
On May 4, 8:40 am, "Matthias Hofmann" <hofm...@anvil-soft.com> wrote:
I have a question regarding the redefinition of template parameters. Why is
it legal to simultaneously declare the following:
template <class T> void f() {};
template <int N> void f() {};
C++ allows functions to be "overloaded" - that is, a program may
declare more than one function with the same name. So whenever an
overloaded function is called, the compiler must select which function
with the name of the function being called - is the best match will
be the function that executes as a result of the call.
while the following cannot be simultaneously declared:
template <class T> struct A {};
template <int N> struct A {};
At least my compiler tells me that in the last declaration, the template
parameter is not compatible with "T"...
Aside from functions, the names of most other entities in C++ may not
be overloaded. Class templates, for example, must each be uniquely
named. Therefore declaring two different class templates named "A" -
as the program above attempts to do - is not allowed.
Moreover, because any problem in computer science can be solved with
another layer of indirection (or so they say), one solution here is to
wrap the int in its own class type:
template <int N>
struct Integer {};
and then use an instantiated Integer template class as the type
argment to instantiate A.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]