⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usi_7948.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>17.1 Using the Traits Technique</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="the_5255.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="exc_0293.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>17.1 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 above.  By using the traits technique, when you want to add a new type to the matrix you simply specialize the traits class, not the entire matrix.  You do no more work than you have to and retain the ability to use the matrix on any reasonable number.</P><A NAME="idx206"><!></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><PRE>template &#60;class Num>class matrix_traits{  // traits functions and literals};template &#60;class Num, class traits>class matrix{  // matrix}class matrix_traits&#60;long> {  // traits functions and literals specific to long};class matrix_traits&#60;int>{  // traits functions and literals specific to int};_ etc.matrix&#60;int, matrix_traits&#60;int> > int_matrix;matrix&#60;long, matrix_traits&#60;long> > long_matrix;</PRE><P>Of course you don't even have to specialize on <SAMP>matrix_traits</SAMP>.  You just have to make sure you provide the interface that <SAMP>matrix</SAMP> expects from its traits template parameter. </P><A NAME="idx207"><!></A><P>Most of the time, the operations contained in a traits class will be static functions so that there's no need to actually instantiate a <SAMP>traits</SAMP> object.</P><A NAME="idx208"><!></A><P>The Standard Library uses this technique to give the <A HREF="../stdlibcr/str_8586.htm"><B><I>string</I></B></A> class maximum flexibility and efficiency across a wide range of types.  The <A HREF="../stdlibcr/cha_3696.htm"><B><I>char_traits</I></B></A> 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 skinny and wide characters, for example <SAMP>Strcpy</SAMP> and <SAMP>wcstrcpy</SAMP>.</P><A NAME="idx209"><!></A><P>See the <A HREF="../stdlibcr/cha_3696.htm"><B><I>char_traits</I></B></A> reference entry for a complete description of the traits class.</P><BR><HR><A HREF="the_5255.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="exc_0293.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -