Article View: comp.std.c++
Article #2231Re: Determining between classes and built-in t
From: Fergus Henderson
Date: Tue, 17 Oct 1995 17:58
Date: Tue, 17 Oct 1995 17:58
59 lines
3059 bytes
3059 bytes
Ethan Henry <ethan@learnix.ca> writes:
>[someone wrote:]
>>>>"Brett W. Denner" <dennerbw@cliffy.lfwc.lockheed.com> 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...
Perhaps because you want to write standard-conforming, portable code?
>from G++ 2.7.0 typeinfo.h:
If you are interested in writing standard-conforming portable
code, it would be more useful to read the draft standard than
to read your particular implementation's header files.
>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 {}
Note that all these names (other than `node_type') are in the implementation's
namespace, since they start with two underscores or with an underscore
and a capital letter. These names are all GNU C++ extensions.
>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.
That may work with g++ 2.7, but it is not portable, and it does
not conform to the draft C++ standard.
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ 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:
<9529103.25016@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> <199510171516.LAA05208@lxotta.learnix.ca>Show Full Headers
Complete Article Headers
Approved: fjh@cs.mu.oz.au Date: 17 Oct 95 17:58:51 GMT From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Message-ID: <9529103.25016@mulga.cs.mu.OZ.AU> Newsgroups: comp.std.c++ Organization: Computer Science, University of Melbourne, Australia Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> <199510171516.LAA05208@lxotta.learnix.ca> Return-Path: <daemon@migs.UCAR.EDU> Subject: Re: Determining between classes and built-in t X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMIPvCeEDnX0m9pzZAQH9MgF9EWAOwhHaMAQUbGKSoNZX4Et5Eye3toSm 2NU0GSJd/vWiES5EnoYqT2vttwlS6e+2 =Vv7J