Topic: Template Legitimacy under ANSI
Author: Jason Merrill <jason@cygnus.com>
Date: 1998/06/20 Raw View
>>>>> Brian Hanley <bshanley@novice.uwaterloo.ca> writes:
> template <class T> class A;
> class A<int>
> {
> int var1;
> int var2;
> public:
> A<int>() : var1(0), var2(0) { return; }
> };
> Is the code legal according to the final ANSI standard?
Yes, except that you need to add 'template <>' before each of the
explicit specializations.
Jason
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bshanley@novice.uwaterloo.ca (Brian Hanley)
Date: 1998/06/18 Raw View
Hi,
I was wondering about the legitimacy of explicitly specifying different
implementations for different instantiations of the same template.
For example, take the following code:
// BEGIN CODE SAMPLE
template <class T> class A;
class A<int>
{
int var1;
int var2;
public:
A<int>() : var1(0), var2(0) { return; }
};
class A<double>
{
long var1;
public:
A<double>() : var1(0L) { return; }
};
// END CODE SAMPLE
Is the code legal according to the final ANSI standard?
It compiles under gcc 2.8.0 ... Does it compile under,
say, Borland, or MSVC? And do you expect it to (continue to be)
supported for the various compilers?
In 'Effective C++', Scott Meyers gave the following implementation
of a <limits.h> function:
inline int numeric_limits<int>::min() throw()
{ return INT_MIN; }
Again, the template implementation obviously varies depending on the
parameter.
Also, then, am I correct in assuming that parameter-specific
implementations of template class member functions are legal?
(I know that this use of templates is certainly not common, and
that it might appear inheritance is a better route than templates,
but I want to know about the legality of the templates nonetheless!)
TIA.
Brian
--
Brian S Hanley | "Fear is the main source of superstition,
2A Systems Design Engineering | and one of the main sources of cruelty.
U of Waterloo /|\ To conquer fear is the beginning of wisdom."
___________________________/_\|/_\__ - Bertrand Russell _____________________
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]