<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <meta name="generator" content="HTML Tidy, see www.w3.org"> <meta http-equiv="Content-Type" content= "text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="../boost.css"> <title>Boost.Python - <boost/python/type_id.hpp></title> <style type="text/css"> p.c4 {font-style: italic} span.c3 {color: #ff0000} h2.c2 {text-align: center} h1.c1 {text-align: center} </style> <table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header"> <tr> <td valign="top" width="300"> <h3><a href="../../../../index.htm"><img height="86" width= "277" alt="C++ Boost" src="../../../../c++boost.gif" border= "0"></a></h3> <td valign="top"> <h1 class="c1"><a href="../index.html">Boost.Python</a></h1> <h2 class="c2">Header <boost/python/type_id.hpp></h2> </table> <hr> <h2>Contents</h2> <dl class="page-index"> <dt><a href="#introduction">Introduction</a> <dt><a href="#classes">Classes</a> <dd> <dl class="page-index"> <dt><a href="#type_info-spec">Class <code>type_info</code></a> <dd> <dl class="page-index"> <dt><a href="#type_info-spec-synopsis">Class <code>type_info</code> synopsis</a> <dt><a href="#type_infospec-ctors">Class <code>type_info</code> constructor</a> <dt><a href="#type_infospec-comparisons">Class <code>type_info</code> comparison functions</a> <dt><a href="#type_infospec-observers">Class <code>type_info</code> observer functions</a> </dl> </dl> <dt><a href="#functions">Functions</a> <dd> <dl class="page-index"> <dt><a href="#type_id-spec">type_id</a> </dl> <dt><a href="#examples">Example</a> </dl> <hr> <h2><a name="introduction"></a>Introduction</h2> <p><code><boost/python/type_id.hpp></code> provides types and functions for runtime type identification like those of of <code><typeinfo></code>. It exists mostly to work around certain compiler bugs and platform-dependent interactions with shared libraries. <h2><a name="classes"></a>Classes</h2> <h3><a name="type_info-spec"></a>Class <code>type_info</code></h3> <p><code>type_info</code> instances identify a type. As <code>std::type_info</code> is specified to (but unlike its implementation in some compilers), <code>boost::python::type_info</code> never represents top-level references or cv-qualification (see section 5.2.8 in the C++ standard). Unlike <code>std::type_info</code>, <code>boost::python::type_info</code> instances are copyable, and comparisons always work reliably across shared library boundaries. <h4><a name="type_info-spec-synopsis"></a>Class type_info synopsis</h4> <pre> namespace boost { namespace python { class type_info : <a href= "../../../utility/operators.htm#totally_ordered1">totally_ordered</a><type_info> { public: // constructor type_info(std::type_info const& = typeid(void)); // comparisons bool operator<(type_info const& rhs) const; bool operator==(type_info const& rhs) const; // observers char const* name() const; }; }} </pre> <h4><a name="type_infospec-ctors">Class <code>type_info</code> constructor</a></h4> <pre> type_info(std::type_info const& = typeid(void)); </pre> <dl class="function-semantics"> <dt><b>Effects:</b> constructs a <code>type_info</code> object which identifies the same type as its argument. <dt><b>Rationale:</b> Since it is occasionally neccessary to make an array of <code>type_info</code> objects a benign default argument is supplied. <span class="c3"><b>Note:</b></span> this constructor does <i>not</i> correct for non-conformance of compiler <code>typeid()</code> implementations. See <code><a href="#type_id-spec">type_id</a></code>, below. </dl> <h4><a name="type_infospec-comparisons">Class <code>type_info</code> comparisons</a></h4> <pre> bool operator<(type_info const& rhs) const; </pre> <dl class="function-semantics"> <dt><b>Effects:</b> yields a total order over <code>type_info</code> objects. </dl> <pre> bool operator==(type_info const& rhs) const; </pre> <dl class="function-semantics"> <dt><b>Returns:</b> <code>true</code> iff the two values describe the same type. </dl> <dl class="function-semantics"> <dt><b>Note:</b> The use of <code><a href= "../../../utility/operators.htm#totally_ordered1">totally_ordered</a><type_info></code> as a private base class supplies operators <code><=</code>, <code>>=</code>, <code>></code>, and <code>!=</code> </dl> <h4><a name="type_infospec-observers">Class <code>type_info</code> observers</a></h4> <pre> char const* name() const; </pre> <dl class="function-semantics"> <dt><b>Returns:</b> The result of calling <code>name()</code> on the argument used to construct the object. </dl> <h2><a name="functions"></a>Functions</h2> <pre> std::ostream& operator<<(std::ostream&s, type_info const&x); </pre> <dl class="function-semantics"> <dt><b>Effects:</b> Writes a description of the type described by to <code>x</code> into <code>s</code>. <dt><b>Rationale:</b> Not every C++ implementation provides a truly human-readable <code>type_info::name()</code> string, but for some we may be able to decode the string and produce a reasonable representation. </dl> <pre> <a name="type_id-spec">template <class T> type_info type_id</a>() </pre> <dl class="function-semantics"> <dt><b>Returns:</b> <code>type_info(typeid(T))</code> <dt><b>Note:</b> On some non-conforming C++ implementations, the code is not actually as simple as described above; the semantics are adjusted to work <i>as-if</i> the C++ implementation were conforming. </dl> <h2><a name="examples"></a>Example</h2> The following example, though silly, illustrates how the <code>type_id</code> facility might be used <pre> #include <boost/python/type_id.hpp> // Returns true iff the user passes an int argument template <class T> bool is_int(T x) { using boost::python::type_id; return type_id<T>() == type_id<int>(); } </pre> <p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> 13 November, 2002 <!--webbot bot="Timestamp" endspan i-checksum="39359" --> <p class="c4">© Copyright <a href= "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. All Rights Reserved.