blob: 957e902a7e370eae4c261420b0efdcd00f011a0c [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>Using the Traits Technique</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="17-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="18.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>17.2 Using the Traits Technique</H2>
<P>To implement a traits parameter for a class, you add it as an extra template parameter to your class. You then supply a class for this parameter that encapsulates all the specific operations. Usually that class is itself a template.</P>
<P>As an example, let's look at the matrix problem described in <A HREF="17-1.html">Section&nbsp;17.1</A>. When you want to add a new type to the matrix, you can use the traits technique and simply specialize the traits class, not the entire matrix. You do no more work than you have to, and you retain the ability to use the matrix on any reasonable number.</P>
<A NAME="idx416"><!></A>
<P>Here's how the matrix traits template and specializations for <SAMP>long</SAMP> and <SAMP>int</SAMP> might look. The example also includes a skeleton of the matrix class that uses the traits template.</P>
<UL><PRE>
template &lt;class Num&gt;
class matrix_traits
{
// traits functions and literals
};
template &lt;class Num, class traits&gt;
class matrix
{
// matrix
}
class matrix_traits&lt;long&gt;
{
// traits functions and literals specific to long
};
class matrix_traits&lt;int&gt;
{
// traits functions and literals specific to int
};
... etc.
std::matrix&lt;int, matrix_traits&lt;int&gt; &gt; int_matrix;
std::matrix&lt;long, matrix_traits&lt;long&gt; &gt; long_matrix;
</PRE></UL>
<P>Of course you don't even have to specialize on <SAMP>matrix_traits</SAMP>. You just have to make sure that you provide the interface that <SAMP>matrix</SAMP> expects from its traits template parameter. </P>
<A NAME="idx417"><!></A>
<P>Most of the time, the operations contained in a traits class are static functions so there's no need to actually instantiate a traits object.</P>
<A NAME="idx418"><!></A>
<P>The C++ Standard Library uses this technique to give the <B><I><A HREF="../stdlibref/basic-string.html">string</A></I></B> class maximum flexibility and efficiency across a wide range of types. The <B><I><A HREF="../stdlibref/char-traits.html">char_traits</A></I></B> traits class provides elementary operations on character arrays. In the simplest case, this means providing <B><I>string</I></B> a <SAMP>wstring</SAMP> with access to the C library functions for narrow and wide characters, for example <SAMP>strcpy</SAMP> and <SAMP>wcstrcpy</SAMP>.</P>
<A NAME="idx419"><!></A>
<P>See the <B><I><A HREF="../stdlibref/char-traits.html">char_traits</A></I></B> entry in the <A HREF="../stdlibref/noframes.html"><I>C++ Standard Library Module Reference Guide</I></A> for a complete description of the traits class.</P>
<BR>
<HR>
<A HREF="17-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="18.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>