Topic: Invalid" argument(s) in template class declaration, how to realize?
Author: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
Date: Fri, 20 Nov 1992 00:14:46 GMT Raw View
Q: When designing a template class, how to make sure that its argument
can only be certain classes or certain predefined types?
Another words, can we build a template class such that it only accepts
certain types as its arguments, while for others it will generate an
error message such as:
This type is not supported by this template.
if you attempt to use them as the argument.
To give you an example( hopefully you will think it's reasonable! )
template<class T>
class Expect_Numerical_In_Nature
{
T Nian;
public:
// ...
};
and suppose that we also have:
class Not_Numerical_In_Nature;
then we will expect the following declaration:
Expect_Numberical_In_Nature<Not_Numerical_In_Nature> Not_Valid;
generate an error message.
How do we provide such a mechanism?
P.S. While I am writing this down, I feel this is probably a hard question,
because, I think, the template of C++ doesn't provide such property?
(Well, just guess, don't get offended if it actually does.) How hard
will be a work around?
--------------------------------------------------------------
A template version of my signature class is under repair.
Symptom: It dies without a warning if I use certain class
as an argument.
xjzhu@math.uwaterloo.ca
--------------------------------------------------------------
Author: pjl@cs.uiuc.edu (Paul Lucas)
Date: Fri, 20 Nov 1992 16:56:47 GMT Raw View
In <BxznCM.K9o@math.uwaterloo.ca> xjzhu@math.uwaterloo.ca (Xiaojun Zhu) writes:
>Q: When designing a template class, how to make sure that its argument
> can only be certain classes or certain predefined types?
*****> You can't. When instantiation takes place, the user will get
an error message that something is inappropriate. If your class
is documented, this shouldn't be a real problem, i.e., the user
has to RTFM on your class.
>How do we provide such a mechanism?
*****> IMHO, there's nothing wrong with the way it's done now.
>P.S. While I am writing this down, I feel this is probably a hard question,
> because, I think, the template of C++ doesn't provide such property?
>(Well, just guess, don't get offended if it actually does.) How hard
>will be a work around?
*****> If you insist, perhaps #pragma's and/or #error's to sugar-coat
the error messages.
--
- Paul J. Lucas University of Illinois
AT&T Bell Laboratories at Urbana-Champaign
Naperville, IL pjl@cs.uiuc.edu
Author: shankar@sgi.com (Shankar Unni)
Date: 24 Nov 1992 01:00:23 GMT Raw View
Xiaojun Zhu (xjzhu@math.uwaterloo.ca) wrote:
> Q: When designing a template class, how to make sure that its argument
> can only be certain classes or certain predefined types?
>
> Another words, can we build a template class such that it only accepts
> certain types as its arguments, while for others it will generate an
> error message such as:
>
> This type is not supported by this template.
You can't do that now, but if the proposal for RTTI (Run-Time Type
Identification) ever gets into the standard in some form, you will be
able to do this by doing a run-time check (usually statically
determinable by the compiler).
--
Shankar Unni E-Mail: shankar@sgi.com
Silicon Graphics Inc. Phone: +1-415-390-2072