You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
6.1 KiB

  1. namespace Eigen {
  2. /** \page TopicTemplateKeyword The template and typename keywords in C++
  3. There are two uses for the \c template and \c typename keywords in C++. One of them is fairly well known
  4. amongst programmers: to define templates. The other use is more obscure: to specify that an expression refers
  5. to a template function or a type. This regularly trips up programmers that use the %Eigen library, often
  6. leading to error messages from the compiler that are difficult to understand.
  7. <b>Table of contents</b>
  8. - \ref TopicTemplateKeywordToDefineTemplates
  9. - \ref TopicTemplateKeywordExample
  10. - \ref TopicTemplateKeywordExplanation
  11. - \ref TopicTemplateKeywordResources
  12. \section TopicTemplateKeywordToDefineTemplates Using the template and typename keywords to define templates
  13. The \c template and \c typename keywords are routinely used to define templates. This is not the topic of this
  14. page as we assume that the reader is aware of this (otherwise consult a C++ book). The following example
  15. should illustrate this use of the \c template keyword.
  16. \code
  17. template <typename T>
  18. bool isPositive(T x)
  19. {
  20. return x > 0;
  21. }
  22. \endcode
  23. We could just as well have written <tt>template &lt;class T&gt;</tt>; the keywords \c typename and \c class have the
  24. same meaning in this context.
  25. \section TopicTemplateKeywordExample An example showing the second use of the template keyword
  26. Let us illustrate the second use of the \c template keyword with an example. Suppose we want to write a
  27. function which copies all entries in the upper triangular part of a matrix into another matrix, while keeping
  28. the lower triangular part unchanged. A straightforward implementation would be as follows:
  29. <table class="example">
  30. <tr><th>Example:</th><th>Output:</th></tr>
  31. <tr><td>
  32. \include TemplateKeyword_simple.cpp
  33. </td>
  34. <td>
  35. \verbinclude TemplateKeyword_simple.out
  36. </td></tr></table>
  37. That works fine, but it is not very flexible. First, it only works with dynamic-size matrices of
  38. single-precision floats; the function \c copyUpperTriangularPart() does not accept static-size matrices or
  39. matrices with double-precision numbers. Second, if you use an expression such as
  40. <tt>mat.topLeftCorner(3,3)</tt> as the parameter \c src, then this is copied into a temporary variable of type
  41. MatrixXf; this copy can be avoided.
  42. As explained in \ref TopicFunctionTakingEigenTypes, both issues can be resolved by making
  43. \c copyUpperTriangularPart() accept any object of type MatrixBase. This leads to the following code:
  44. <table class="example">
  45. <tr><th>Example:</th><th>Output:</th></tr>
  46. <tr><td>
  47. \include TemplateKeyword_flexible.cpp
  48. </td>
  49. <td>
  50. \verbinclude TemplateKeyword_flexible.out
  51. </td></tr></table>
  52. The one line in the body of the function \c copyUpperTriangularPart() shows the second, more obscure use of
  53. the \c template keyword in C++. Even though it may look strange, the \c template keywords are necessary
  54. according to the standard. Without it, the compiler may reject the code with an error message like "no match
  55. for operator<".
  56. \section TopicTemplateKeywordExplanation Explanation
  57. The reason that the \c template keyword is necessary in the last example has to do with the rules for how
  58. templates are supposed to be compiled in C++. The compiler has to check the code for correct syntax at the
  59. point where the template is defined, without knowing the actual value of the template arguments (\c Derived1
  60. and \c Derived2 in the example). That means that the compiler cannot know that <tt>dst.triangularPart</tt> is
  61. a member template and that the following &lt; symbol is part of the delimiter for the template
  62. parameter. Another possibility would be that <tt>dst.triangularPart</tt> is a member variable with the &lt;
  63. symbol refering to the <tt>operator&lt;()</tt> function. In fact, the compiler should choose the second
  64. possibility, according to the standard. If <tt>dst.triangularPart</tt> is a member template (as in our case),
  65. the programmer should specify this explicitly with the \c template keyword and write <tt>dst.template
  66. triangularPart</tt>.
  67. The precise rules are rather complicated, but ignoring some subtleties we can summarize them as follows:
  68. - A <em>dependent name</em> is name that depends (directly or indirectly) on a template parameter. In the
  69. example, \c dst is a dependent name because it is of type <tt>MatrixBase&lt;Derived1&gt;</tt> which depends
  70. on the template parameter \c Derived1.
  71. - If the code contains either one of the contructions <tt>xxx.yyy</tt> or <tt>xxx-&gt;yyy</tt> and \c xxx is a
  72. dependent name and \c yyy refers to a member template, then the \c template keyword must be used before
  73. \c yyy, leading to <tt>xxx.template yyy</tt> or <tt>xxx-&gt;template yyy</tt>.
  74. - If the code contains the contruction <tt>xxx::yyy</tt> and \c xxx is a dependent name and \c yyy refers to a
  75. member typedef, then the \c typename keyword must be used before the whole construction, leading to
  76. <tt>typename xxx::yyy</tt>.
  77. As an example where the \c typename keyword is required, consider the following code in \ref TutorialSparse
  78. for iterating over the non-zero entries of a sparse matrix type:
  79. \code
  80. SparseMatrixType mat(rows,cols);
  81. for (int k=0; k<mat.outerSize(); ++k)
  82. for (SparseMatrixType::InnerIterator it(mat,k); it; ++it)
  83. {
  84. /* ... */
  85. }
  86. \endcode
  87. If \c SparseMatrixType depends on a template parameter, then the \c typename keyword is required:
  88. \code
  89. template <typename T>
  90. void iterateOverSparseMatrix(const SparseMatrix<T>& mat;
  91. {
  92. for (int k=0; k<m1.outerSize(); ++k)
  93. for (typename SparseMatrix<T>::InnerIterator it(mat,k); it; ++it)
  94. {
  95. /* ... */
  96. }
  97. }
  98. \endcode
  99. \section TopicTemplateKeywordResources Resources for further reading
  100. For more information and a fuller explanation of this topic, the reader may consult the following sources:
  101. - The book "C++ Template Metaprogramming" by David Abrahams and Aleksey Gurtovoy contains a very good
  102. explanation in Appendix B ("The typename and template Keywords") which formed the basis for this page.
  103. - http://pages.cs.wisc.edu/~driscoll/typename.html
  104. - http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
  105. - http://www.comeaucomputing.com/techtalk/templates/#templateprefix
  106. - http://www.comeaucomputing.com/techtalk/templates/#typename
  107. */
  108. }