tti_func_templates.qbk 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [/
  2. (C) Copyright Edward Diener 2011
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:tti_func_templates Introspecting Function Templates]
  8. The one nested element which the TTI library does not introspect is
  9. function templates.
  10. Function templates, like functions, can be member function templates or
  11. static member function templates. In this respect they are related to
  12. functions. Function templates represent a family of possible functions.
  13. In this respect they are similar to class templates, which represent a
  14. family of possible class types.
  15. The technique for introspecting class templates in the TTI library is taken
  16. from the implementation of the technique in the Boost MPL library. In the case
  17. of `BOOST_TTI_HAS_TEMPLATE` it directly uses the Boost MPL library functionality
  18. while in the case of `BOOST_TTI_HAS_TEMPLATE_CHECK_PARAMS` it replicates much
  19. of the technique in the Boost MPL library. The technique depends directly on
  20. the fact that in C++ we can pass a template as a parameter to another template
  21. using what is called a "template template" parameter type.
  22. One obvious thing about a template template parameter type is that it is a
  23. class template. For whatever historical or technical reasons, no one has ever
  24. proposed that C++ have a way of passing a function template directly as a template
  25. parameter, perhaps to be called a "function template template" parameter type.
  26. I personally think this would be a good addition to C++ and would
  27. make the ability of passing a template as a parameter to another template
  28. more orthogonal, since both class templates and function templates would be supported.
  29. My efforts to discuss this on the major C++ newsgroups have
  30. met with arguments both against its practical usage and the justification
  31. that one can pass a function template to another template nested in a non-template
  32. class, which serves as a type. But of course we can do the same thing with class templates,
  33. which is in fact what Boost MPL does to pass templates as metadata, yet we still have
  34. template template parameters as class templates.
  35. Nonetheless the fact that we can pass class templates as a template parameter but not
  36. function templates as a template parameter is the major factor why there is no really good
  37. method for introspecting function templates at compile time.
  38. [heading Instantiating a nested function template]
  39. There is, however, an alternate but less certain way of introspecting a function template.
  40. I will endeavor to explain why this way is not currently included in the TTI library,
  41. but first I will explain what it is.
  42. It is possible to check whether some particular [*instantiation] of a nested function
  43. template exists at compile-time without generating a compiler error. Although checking if
  44. some particular instantiation of a nested function template exists at compile-time does
  45. not prove that the nested function template itself does or does not exist,
  46. since the instantiation itself may be incorrect and fail even when the nested function
  47. template exists, it provides a partial, if flawed, means of checking.
  48. The code to do this for member function templates looks like this
  49. ( similar code also exists for static member function templates ):
  50. template
  51. <
  52. class C,
  53. class T
  54. >
  55. struct TestFunctionTemplate
  56. {
  57. typedef char Bad;
  58. struct Good { char x[2]; };
  59. template<T> struct helper;
  60. template<class U> static Good check(helper<&U::template SomeFuncTemplateName<int,long,double> > *);
  61. template<class U> static Bad check(...);
  62. static const bool value=sizeof(check<C>(0))==sizeof(Good);
  63. };
  64. where 'SomeFuncTemplateName' is the name of the nested function template,
  65. followed by some parameters to instantiate it. The 'class C' is the type of
  66. the enclosing class and the 'class T' is the type of the instantiated member
  67. function template as a member function.
  68. As an example if we had:
  69. struct AType
  70. {
  71. template<class X,class Y,class Z> double SomeFuncTemplateName(X,Y *,Z &) { return 0.0; }
  72. };
  73. then instantiating the above template with:
  74. TestFunctionTemplate
  75. <
  76. AType,
  77. double (AType::*)(int,long *,double &)
  78. >
  79. would provide a compile-time boolean value which would tell us whether the
  80. nested member function template exists for the particular instantiation
  81. provided above. Furthermore, through the use of a macro, the TTI library
  82. could provide the means for specifying the name of the nested member function
  83. template ('SomeFuncTemplateName' above) and its set of instantiated
  84. parameters ('int,long,double' above) for generating the template.
  85. So why does not the TTI library not provide at least this much functionality for
  86. introspecting member function templates, even if it represents a partially flawed
  87. way of doing so ?
  88. The reason is stunningly disappointing. Although the above code is perfectly correct C++
  89. code ( 'clang' works correctly ), two of the major C++ compilers, in all of their different
  90. releases, can not handle the above code correctly. Both gcc ( g++ ) and Visual C++ incorrectly
  91. choose the wrong 'check' function even when the correct 'check' function applies ( Comeau C++
  92. also fails but I am less concerned about that compiler since it is not used nearly as much as
  93. the other two ). All my attempts at alternatives to the above code have also failed. The problems
  94. with both compilers, in this regard, can be seen more easily with this snippet:
  95. struct AType
  96. {
  97. template<class AA> void SomeFuncTemplate() { }
  98. };
  99. template<class T>
  100. struct Test
  101. {
  102. template<T> struct helper;
  103. template<class U> static void check(helper<&U::template SomeFuncTemplate<int> > *) { }
  104. };
  105. int main()
  106. {
  107. Test< void (AType::*)() >::check<AType>(0);
  108. return 0;
  109. }
  110. Both compilers report compile errors with this perfectly correct code,
  111. gcc:
  112. error: no matching function for call to 'Test<void (AType::*)()>::check(int)'
  113. and msvc:
  114. error C2770: invalid explicit template argument(s) for 'void Test<T>::check(Test<T>::helper<&U::SomeFuncTemplate<int>> *)'
  115. There is a workaround for these compiler problems, which is to hardcode the name
  116. of the enclosing class, via a macro, in the generated template rather than pass it as a
  117. template type. In that case both compilers can handle both the member function code and
  118. the code snippet above correctly. In essence, when the line:
  119. template<class U> static void check(helper<&U::template SomeFuncTemplate<int> > *) { }
  120. gets replaced by:
  121. template<class U> static void check(helper<&AType::template SomeFuncTemplate<int> > *) { }
  122. both gcc and Visual C++ work correctly. The same goes for the 'check' line in the
  123. 'TestFunctionTemplate' above.
  124. But the workaround destroys one of the basic tenets of the TTI library, which is that
  125. the enclosing class be passed as a template parameter, especially as the enclosing class
  126. need not actually exist ( see `BOOST_TTI_MEMBER_TYPE` and the previous discussion of 'Nested Types' ),
  127. without producing a compiler error. So I have decided not to implement even this methodology to
  128. introspect nested function templates in the TTI library.
  129. [endsect]