Article View: comp.std.c++
Article #2228Re: Determining between classes and built-in t
From: Ethan Henry
Date: Tue, 17 Oct 1995 16:19
Date: Tue, 17 Oct 1995 16:19
64 lines
3108 bytes
3108 bytes
In article <45ns7a$cir@panix.com> you write:
>In article <45mor5$fms@jupiter.SJSU.EDU>,
>Cay S. Horstmann <horstman@jupiter.SJSU.EDU> wrote:
>>clamage@Eng.sun.com (Steve Clamage) wrote:
>>
>>>In article 100000@gusak, "Brett W. Denner" <dennerbw@cliffy.lfwc.lockheed.co
m>
>> writes:
>>>>Is there a way to identify whether a data type in a template function is a
>>>>class or a built-in type? For example,
>
>How about using template specialization?
> template<class T> bool builtin() { return false; }
> template<class T> bool builtin<char>() { return true; }
Why not use RTTI ? typeid() will tell you the info you need...
from G++ 2.7.0 typeinfo.h:
// RTTI support: typeinfo.h
// Written by Kung Hsu based upon the specification in the 20 September 1994
// C++ working paper, ANSI document X3J16/94-0158.
class type_info {
private:
// assigning type_info is not supported. made private.
type_info& operator=(const type_info&);
type_info(const type_info&);
public:
enum node_type {
_RTTI_BUILTIN_TYPE, // builtin type
_RTTI_USER_TYPE, // user defined type
_RTTI_CLASS_TYPE, // class type
_RTTI_POINTER_TYPE, // pointer type
_RTTI_ATTR_TYPE, // attribute type for const and volatile
_RTTI_FUNC_TYPE, // function type
_RTTI_PTMF_TYPE, // pointer to member function type
_RTTI_PTMD_TYPE // pointer to member data type
};
// return node type of the object
virtual node_type __rtti_get_node_type() const {}
<remainder deleted>
so to determine the type of object x...
if(typeid(x).__rtti_get_node_type() == _RTTI_BUILTIN_TYPE)
cout << "x is a builtin type" << endl;
else if if(typeid(x).__rtti_get_node_type() == _RTTI_CLASS_TYPE)
cout << "x is a class type" << endl;
etc.
Ethan
--
Ethan Henry ethan@learnix.ca
Instructor, Learnix Inc. Visit http://www.learnix.ca
"Don't try to outweird me - I get weirder stuff than you free in my
breakfast cereal." -Z. Beeblebrox, quoted in the Design & Evolution of C++
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<199510171516.LAA05208@lxotta.learnix.ca>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 17 Oct 95 16:19:36 GMT Approved: fjh@cs.mu.oz.au X-Original-Date: Tue, 17 Oct 1995 11:16:02 -0400 From: Ethan Henry <ethan@learnix.ca> Message-ID: <199510171516.LAA05208@lxotta.learnix.ca> Subject: Re: Determining between classes and built-in t Newsgroups: comp.std.c++ In-Reply-To: <45ns7a$cir@panix.com> References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> Organization: Learnix X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMIPXpeEDnX0m9pzZAQHkFQF8DIywoRNbt2rYL3bgrCS7yGEYVKPl/2k3 OpydaLPHoXPPKXk3QCRLJIuTlGTX/Ad5 =jWvH