typeof.qbk 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. [library Boost.Typeof
  2. [authors [Vertleyb, Arkadiy], [Holt, Peder]]
  3. [copyright 2004 2005 Arkadiy Vertleyb, Peder Holt]
  4. [license
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. <ulink url="http://www.boost.org/LICENSE_1_0.txt">
  8. http://www.boost.org/LICENSE_1_0.txt
  9. </ulink>)
  10. ]
  11. [id typeof]
  12. [last-revision $Date$]
  13. ]
  14. [section:moti Motivation]
  15. [c++]
  16. Today many template libraries supply object generators to simplify object creation
  17. by utilizing the C++ template argument deduction facility. Consider `std::pair`.
  18. In order to instantiate this class template and create a temporary object of this instantiation,
  19. one has to supply template parameters, as well as parameters to the constructor:
  20. std::pair<int, double>(5, 3.14159);
  21. To avoid this duplication, STL supplies the `std::make_pair` object generator.
  22. When it is used, the types of template parameters are deduced from supplied function arguments:
  23. std::make_pair(5, 3.14159);
  24. For the temporary objects it is enough. However, when a named object needs to be allocated,
  25. the problem appears again:
  26. std::pair<int, double> p(5, 3.14159);
  27. The object generator no longer helps:
  28. std::pair<int, double> p = std::make_pair(5, 3.14159);
  29. It would be nice to deduce the type of the object (on the left) from the expression
  30. it is initialized with (on the right), but the current C++ syntax does not allow for this.
  31. The above example demonstrates the essence of the problem but does not demonstrate its scale.
  32. Many libraries, especially expression template libraries, create objects of really complex types,
  33. and go a long way to hide this complexity behind object generators. Consider a nit Boost.Lambda functor:
  34. _1 > 15 && _2 < 20
  35. If one wanted to allocate a named copy of such an innocently looking functor,
  36. she would have to specify something like this:
  37. lambda_functor<
  38. lambda_functor_base<
  39. logical_action<and_action>,
  40. tuple<
  41. lambda_functor<
  42. lambda_functor_base<
  43. relational_action<greater_action>,
  44. tuple<
  45. lambda_functor<placeholder<1> >,
  46. int const
  47. >
  48. >
  49. >,
  50. lambda_functor<
  51. lambda_functor_base<
  52. relational_action<less_action>,
  53. tuple<
  54. lambda_functor<placeholder<2> >,
  55. int const
  56. >
  57. >
  58. >
  59. >
  60. >
  61. >
  62. f = _1 > 15 && _2 < 20;
  63. Not exactly elegant. To solve this problem (as well as some other problems),
  64. the C++ standard committee is considering
  65. a few additions to the standard language, such as `typeof/decltype` and `auto` (see
  66. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf
  67. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf]).
  68. The `typeof` operator (or `decltype`, which is a slightly different flavor of `typeof`)
  69. allows one to determine the type of an expression at compile time. Using `typeof`,
  70. the above example can be simplified drastically:
  71. typeof(_1 > 15 && _2 < 20) f = _1 > 15 && _2 < 20;
  72. Much better, but some duplication still exists. The `auto` type solves the rest of the problem:
  73. auto f = _1 > 15 && _2 < 20;
  74. The purpose of the Boost.Typeof library is to provide a library-based solution,
  75. which could be used until the language-based facility is added to the Standard
  76. and becomes widely available.
  77. [endsect]
  78. [section:tuto Tutorial]
  79. To start using typeof include the typeof header:
  80. #include <boost/typeof/typeof.hpp>
  81. To deduce the type of an expression at compile time
  82. use the `BOOST_TYPEOF` macro:
  83. namespace ex1
  84. {
  85. typedef BOOST_TYPEOF(1 + 0.5) type;
  86. BOOST_STATIC_ASSERT((is_same<type, double>::value));
  87. }
  88. In the dependent context use `BOOST_TYPEOF_TPL` instead of `BOOST_TYPEOF`:
  89. namespace ex2
  90. {
  91. template<class T, class U>
  92. BOOST_TYPEOF_TPL(T() + U()) add(const T& t, const U& u)
  93. {
  94. return t + u;
  95. };
  96. typedef BOOST_TYPEOF(add('a', 1.5)) type;
  97. BOOST_STATIC_ASSERT((is_same<type, double>::value));
  98. }
  99. The above examples are possible because the Typeof Library knows about
  100. primitive types, such as `int`, `double`, `char`, etc. The Typeof Library also
  101. knows about most types and templates defined by the
  102. Standard C++ Library, but the appropriate headers need to be included
  103. to take advantage of this:
  104. #include <boost/typeof/std/utility.hpp>
  105. namespace ex3
  106. {
  107. BOOST_AUTO(p, make_pair(1, 2));
  108. BOOST_STATIC_ASSERT((is_same<BOOST_TYPEOF(p), pair<int, int> >::value));
  109. }
  110. Here `<boost/typeof/std/utility.hpp>` includes `<utility>` and contains
  111. knowledge about templates defined there. This naming convention
  112. applies in general, for example to let the Typeof Library handle `std::vector`,
  113. include `<boost/typeof/std/vector.hpp>`, etc.
  114. To deduce the type of a variable from the expression, this variable
  115. is initialized with, use the `BOOST_AUTO` macro (or `BOOST_AUTO_TPL`
  116. in a dependent context:
  117. #include <boost/typeof/std/string.hpp>
  118. namespace ex4
  119. {
  120. BOOST_AUTO(p, new int[20]);
  121. BOOST_STATIC_ASSERT((is_same<BOOST_TYPEOF(p), int*>::value));
  122. }
  123. Both `BOOST_TYPEOF` and `BOOST_AUTO` strip top-level qualifiers.
  124. Therefore, to allocate for example a reference, it has to be specified explicitly:
  125. namespace ex5
  126. {
  127. string& hello()
  128. {
  129. static string s = "hello";
  130. return s;
  131. }
  132. BOOST_AUTO(&s, hello());
  133. }
  134. To better understand this syntax, note that this gets expanded into:
  135. BOOST_TYPEOF(hello()) &s = hello();
  136. If your define your own type, the Typeof Library cannot handle it
  137. unless you let it know about this type. You tell the Typeof Library
  138. about a type (or template) by the means of "registering" this type/template.
  139. Any source or header file where types/templates are registered has to
  140. contain the following line before any registration is done:
  141. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  142. After this a type can be registered:
  143. namespace ex6
  144. {
  145. struct MyType
  146. {};
  147. }
  148. BOOST_TYPEOF_REGISTER_TYPE(ex6::MyType)
  149. The registration must be done from the context of global namespace;
  150. fully qualified type name has to be used.
  151. Any number of types can be registered in one file, each on a separate line.
  152. Once your type is registered, the Typeof Library can handle it in any context:
  153. namespace ex6
  154. {
  155. typedef BOOST_TYPEOF(make_pair(1, MyType())) type;
  156. BOOST_STATIC_ASSERT((is_same<type, pair<int, MyType> >::value));
  157. }
  158. A template is registered by specifying its fully qualified name,
  159. and describing its parameters. In the simplest case, when all parameters
  160. are type parameters, only their number needs to be specified:
  161. namespace ex7
  162. {
  163. template<class T, class U>
  164. struct MyTemplate
  165. {};
  166. }
  167. BOOST_TYPEOF_REGISTER_TEMPLATE(ex7::MyTemplate, 2)
  168. namespace ex7
  169. {
  170. typedef BOOST_TYPEOF(make_pair(1, MyTemplate<int, ex6::MyType>())) type;
  171. BOOST_STATIC_ASSERT((is_same<type,
  172. pair<int, MyTemplate<int, ex6::MyType> >
  173. >::value));
  174. }
  175. When a template has integral template parameters, all parameters need
  176. to be described in the preprocessor sequence:
  177. namespace ex8
  178. {
  179. template<class T, int n>
  180. struct MyTemplate
  181. {};
  182. }
  183. BOOST_TYPEOF_REGISTER_TEMPLATE(ex8::MyTemplate, (class)(int))
  184. namespace ex8
  185. {
  186. typedef BOOST_TYPEOF(make_pair(1, MyTemplate<ex7::MyTemplate<ex6::MyType, int>, 0>())) type;
  187. BOOST_STATIC_ASSERT((is_same<type,
  188. pair<int, MyTemplate<ex7::MyTemplate<ex6::MyType, int>, 0> >
  189. >::value));
  190. }
  191. Please see the reference for more details.
  192. [endsect]
  193. [section:refe Reference]
  194. [section:auto AUTO, AUTO_TPL]
  195. The `BOOST_AUTO` macro emulates the proposed `auto` keyword in C++.
  196. [h4 Usage]
  197. BOOST_AUTO(var,expr)
  198. BOOST_AUTO_TPL(var,expr)
  199. [variablelist Arguments
  200. [[var][a variable to be initialized with the expression]]
  201. [[expr][a valid c++ expression]]
  202. ]
  203. [h4 Remarks]
  204. If you want to use `auto` in a template-context, use `BOOST_AUTO_TPL(expr)`,
  205. which takes care of the `typename` keyword inside the `auto` expression.
  206. [h4 Sample Code]
  207. int main()
  208. {
  209. length::meter a(5);
  210. force::newton b(6);
  211. BOOST_AUTO(c, a * b);
  212. }
  213. [endsect]
  214. [section:compl COMPLIANT]
  215. The `BOOST_TYPEOF_COMPLIANT` macro can be used to force the emulation mode.
  216. Define it if your compiler by default uses another mode, such as native `typeof`
  217. or Microsoft-specific trick, but you want to use the emulation mode,
  218. for example for portability reasons.
  219. [endsect]
  220. [section:incr INCREMENT_REGISTRATION_GROUP]
  221. The `BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP` macro ensures that type registrations
  222. in different header files receive unique identifiers.
  223. [h4 Usage]
  224. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  225. [h4 Remarks]
  226. specified once in every cpp/hpp file where any registration is performed,
  227. before any registration.
  228. [h4 Sample Code]
  229. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  230. class X;
  231. BOOST_TYPEOF_REGISTER_TYPE(X)
  232. [endsect]
  233. [section:inte INTEGRAL]
  234. The `BOOST_TYPEOF_INTEGRAL` macro is used when registering an integral
  235. template parameter using `BOOST_TYPEOF_REGISTER_TEMPLATE`.
  236. Useful for `enum`s and dependent integral template parameters.
  237. [h4 Usage]
  238. BOOST_TYPEOF_INTEGRAL(x)
  239. [variablelist Arguments
  240. [[x][a fully qualified integral type or enum]]
  241. ]
  242. [h4 Remarks]
  243. A short syntax has been implemented for the built in types
  244. (int, bool, long, unsigned long, etc.)
  245. Other non-type template parameters (e.g. pointer to member)
  246. are not supported.
  247. [h4 Sample Code]
  248. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  249. namespace foo
  250. {
  251. enum color {red, green, blue};
  252. template<color C0,typename T1>
  253. class class_with_enum {};
  254. template<typename T0,T0 I1>
  255. class class_with_dependent_non_type {};
  256. }
  257. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_enum,
  258. (BOOST_TYPEOF_INTEGRAL(foo::color))
  259. (typename)
  260. )
  261. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_dependent_non_type,
  262. (typename)
  263. (BOOST_TYPEOF_INTEGRAL(P0))
  264. )
  265. [endsect]
  266. [section:limit_func LIMIT_FUNCTION_ARITY]
  267. The `BOOST_TYPEOF_LIMIT_FUNCTION_ARITY` macro defines how many parameters
  268. are supported for functios, and applies to functions, function pointers,
  269. function references, and member function pointers. The default value is 10.
  270. Redefine if you want the Typeof Library to handle functions with more parameters.
  271. [endsect]
  272. [section:messages MESSAGES]
  273. Define `BOOST_TYPEOF_MESSAGE` before including boost/typeof/typeof.hpp to
  274. include messages "using typeof emulation" and "using native typeof".
  275. By default, these messages will not be displayed.
  276. [endsect]
  277. [section:limit_size LIMIT_SIZE]
  278. The `BOOST_TYPEOF_LIMIT_SIZE` macro defines the size of the compile-time sequence
  279. used to encode a type. The default value is 50. Increase it if you want
  280. the Typeof Library to handle very complex types, although this
  281. possibility is limited by the maximum number of template parameters supported
  282. by your compiler. On the other hand, if you work only with very simple types,
  283. decreasing this number may help to boost compile-time performance.
  284. [endsect]
  285. [section:regtype REGISTER_TYPE]
  286. The `BOOST_TYPEOF_REGISTER_TYPE` macro informs the Typeof Library
  287. about the existence of a type
  288. [h4 Usage]
  289. BOOST_TYPEOF_REGISTER_TYPE(x)
  290. [variablelist Arguments
  291. [[x][a fully qualified type]]
  292. ]
  293. [h4 Remarks]
  294. Must be used in the global namespace
  295. [h4 Sample Code]
  296. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  297. namespace foo
  298. {
  299. class bar {};
  300. enum color {red, green, blue};
  301. }
  302. BOOST_TYPEOF_REGISTER_TYPE(foo::bar)
  303. BOOST_TYPEOF_REGISTER_TYPE(foo::color)
  304. [endsect]
  305. [section:regtemp REGISTER_TEMPLATE]
  306. The `BOOST_TYPEOF_REGISTER_TEMPLATE` macro informs the Typeof Library
  307. about the existence of a template and describes its parameters
  308. [h4 Usage]
  309. BOOST_TYPEOF_REGISTER_TEMPLATE(x, n)
  310. BOOST_TYPEOF_REGISTER_TEMPLATE(x, seq)
  311. [variablelist Arguments
  312. [[x][a fully qualified template]]
  313. [[n][the number of template arguments. Only valid if all template arguments are typenames]]
  314. [[seq][a sequence of template arguments. Must be used when integral or template template parameters are present]]
  315. ]
  316. [h4 Remarks]
  317. Must be used in the global namespace.
  318. The library allows registration of templates with type, integral,
  319. and template template parameters:
  320. * A type template parameter is described by the `(class)` or `(typename)` sequence element
  321. * A template parameter of a well-known integral type can be described by
  322. simply supplying its type, like `(unsigned int)`.
  323. The following well-known integral types are supported:
  324. * `[signed/unsigned] char`
  325. * `[unsigned] short`
  326. * `[unsigned] int`
  327. * `[unsigned] long`
  328. * `unsigned`
  329. * `bool`
  330. * `size_t`
  331. * Enums and typedefs of integral types, need to be described explicitly
  332. with the `BOOST_TYPEOF_INTEGRAL` macro, like `(BOOST_TYPEOF_INTEGRAL(MyEnum))`
  333. * Template template parameters are described with the `BOOST_TYPEOF_TEMPLATE` macro,
  334. like: `(BOOST_TYPEOF_TEMPLATE((class)(unsigned int)))`.
  335. In case of all type parameters this can be shortened to something like `(BOOST_TYPEOF_TEMPLATE(2))`.
  336. The nested template template parameters are not supported.
  337. [h4 Sample Code]
  338. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  339. namespace foo
  340. {
  341. template<typename T0, typename T1>
  342. class simple_template {};
  343. template<typename T0, int I1>
  344. class class_with_integral_constant {};
  345. }
  346. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::simple_template, 2)
  347. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_integral_constant, (typename)(int))
  348. [endsect]
  349. [section:temp TEMPLATE]
  350. The `BOOST_TYPEOF_TEMPLATE` macro is used when registering template template parameters
  351. using `BOOST_TYPEOF_REGISTER_TEMPLATE`.
  352. [h4 Usage]
  353. BOOST_TYPEOF_TEMPLATE(n)
  354. BOOST_TYPEOF_TEMPLATE(seq)
  355. [variablelist Arguments
  356. [[n][the number of template arguments. Only valid if all template arguments are typenames]]
  357. [[seq][a sequence of template arguments. Must be used when there are integral constants in the nested template]]
  358. ]
  359. [h4 Remarks]
  360. Can not be used to register nested template template parameters.
  361. [h4 Sample Code]
  362. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  363. namespace foo
  364. {
  365. enum color {red, green, blue};
  366. template<color C0, template<typename> class T1>
  367. class nested_template_class {};
  368. template<template<typename, unsigned char> class T1>
  369. class nested_with_integral {};
  370. }
  371. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::nested_template_class,
  372. (foo::color)
  373. (BOOST_TYPEOF_TEMPLATE(1))
  374. )
  375. BOOST_TYPEOF_REGISTER_TEMPLATE(foo::nested_with_integral,
  376. (BOOST_TYPEOF_TEMPLATE((typename)(unsigned char)))
  377. )
  378. [endsect]
  379. [section:typo TYPEOF, TYPEOF_TPL]
  380. The `BOOST_TYPEOF` macro calculates the type of an expression,
  381. but removes the top-level qualifiers, `const&`
  382. [h4 Usage]
  383. BOOST_TYPEOF(expr)
  384. BOOST_TYPEOF_TPL(expr)
  385. [variablelist Arguments
  386. [[expr][a valid c++ expression that can be bound to const T&]]
  387. ]
  388. [h4 Remarks]
  389. If you want to use `typeof` in a template-context, use `BOOST_TYPEOF_TPL(expr)`,
  390. which takes care of `typename` inside the `typeof` expression.
  391. [h4 Sample Code]
  392. template<typename A, typename B>
  393. struct result_of_conditional
  394. {
  395. typedef BOOST_TYPEOF_TPL(true?A():B()) type;
  396. };
  397. template<typename A, typename B>
  398. result_of_conditional<A, B>::type min(const A& a,const B& b)
  399. {
  400. return a < b ? a : b;
  401. }
  402. [endsect]
  403. [section:typn TYPEOF_NESTED_TYPEDEF, TYPEOF_NESTED_TYPEDEF_TPL]
  404. The `TYPEOF_NESTED_TYPEDEF` macro works in much the same way as the 'TYPEOF' macro does, but
  405. workarounds several compiler deficiencies.
  406. [h4 Usage]
  407. BOOST_TYPEOF_NESTED_TYPEDEF(name,expr)
  408. BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr)
  409. [variablelist Arguments
  410. [[name][a valid identifier to nest the typeof operation inside]
  411. [expr][a valid c++ expression that can be bound to const T&]]
  412. ]
  413. [h4 Remarks]
  414. 'typeof_nested_typedef' nests the 'typeof' operation inside a struct. By doing this, the 'typeof' operation
  415. can be split into two steps, deconfusing several compilers (notably VC7.1 and VC8.0) on the way.
  416. This also removes the limitation imposed by `BOOST_TYPEOF_LIMIT_SIZE` and allows you to use 'typeof' on much
  417. larger expressions.
  418. If you want to use `typeof_nested_typedef` in a template-context, use `BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr)`,
  419. which takes care of `typename` inside the `typeof` expression.
  420. 'typeof_nested_typedef' can not be used at function/block scope.
  421. [h4 Sample Code]
  422. template<typename A, typename B>
  423. struct result_of_conditional
  424. {
  425. BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested,true?A():B())
  426. typedef typename nested::type type;
  427. };
  428. template<typename A, typename B>
  429. result_of_conditional<A, B>::type min(const A& a,const B& b)
  430. {
  431. return a < b ? a : b;
  432. }
  433. [endsect]
  434. [endsect]
  435. [section:other Other considerations and tips]
  436. [section:natem Native typeof support and emulation]
  437. Many compilers support typeof already, most noticeable GCC and Metrowerks.
  438. Igor Chesnokov discovered a method that allows to implement `typeof`
  439. on the VC series of compilers. It uses a bug in the Microsoft compiler
  440. that allows a nested class of base to be defined in a class derived from base:
  441. template<int ID> struct typeof_access
  442. {
  443. struct id2type; //not defined
  444. };
  445. template<class T, int ID> struct typeof_register : typeof_access
  446. {
  447. // define base's nested class here
  448. struct typeof_access::id2type
  449. {
  450. typedef T type;
  451. };
  452. };
  453. //Type registration function
  454. typeof_register<T, compile-time-constant> register_type(const T&);
  455. //Actually register type by instantiating typeof_register for the correct type
  456. sizeof(register_type(some-type));
  457. //Use the base class to access the type.
  458. typedef typeof_access::id2type::type type;
  459. Peder Holt adapted this method to VC7.0, where the nested class
  460. is a template class that is specialized in the derived class.
  461. In VC8.0, it seemed that all the bug-featire had been fixed, but
  462. Steven Watanabe managed to implement a more rigorous version of the VC7.0 fix that
  463. enables 'typeof' to be supported 'natively' here as well.
  464. For many other compilers neither native `typeof` support
  465. nor the trick described above is an option. For such compilers
  466. the emulation method is the only way of implementing `typeof`.
  467. According to a rough estimate, at the time of this writing
  468. the introduction of the `typeof`, `auto`, etc., into the C++ standard
  469. may not happen soon. Even after it's done, some time still has to pass
  470. before most compilers implement this feature. But even after that,
  471. there always are legacy compilers to support (for example now, in 2005,
  472. many people are still using VC6, long after VC7.x, and even VC8.0 beta became available).
  473. Considering extreme usefulness of the feature right now,
  474. it seems to make sense to implement it at the library level.
  475. The emulation mode seems to be important even if a better option is present
  476. on some particular compiler. If a library author wants to develop portable
  477. code using `typeof`, she needs to use emulation mode and register her types and
  478. templates. Those users who have a better option can still take
  479. advantage of it, since the registration macros are defined as no-op on
  480. such compilers, while the users for whom emulation is the only option will use it.
  481. The other consideration applies to the users of VC7.1. Even though the more
  482. convenient `typeof` trick is available, the possibility of upgrade to VC8,
  483. where emulation remains the only option, should be considered.
  484. The emulation mode can be forced on the compilers that don't use it
  485. by default by defining the `BOOST_TYPEOF_COMPLIANT` symbol:
  486. g++ -D BOOST_TYPEOF_COMPLIANT -I \boost\boost_1_32_0 main.cpp
  487. [endsect]
  488. [section:parties The three participating parties]
  489. The Lambda example from the Motivation section requires the following registration:
  490. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  491. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::tuples::tuple, 2);
  492. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::lambda_functor, 1);
  493. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::lambda_functor_base, 2);
  494. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::relational_action, 1);
  495. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::logical_action, 1);
  496. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::other_action, 1);
  497. BOOST_TYPEOF_REGISTER_TYPE(boost::lambda::greater_action);
  498. BOOST_TYPEOF_REGISTER_TYPE(boost::lambda::less_action);
  499. BOOST_TYPEOF_REGISTER_TYPE(boost::lambda::and_action);
  500. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::lambda::placeholder, (int));
  501. It may seem that the price for the ability to discover the expression's type
  502. is too high: rather large amount of registration is required.
  503. However note that all of the above registration is done only once,
  504. and after that, any combination of the registered types and templates
  505. would be handled. Moreover, this registration is typically done
  506. not by the end-user, but rather by a layer on top of some library
  507. (in this example -- Boost.Lambda).
  508. When thinking about this, it's helpful to consider three parties: the typeof facility,
  509. the library (probably built on expression templates principle), and the end-user.
  510. The typeof facility is responsible for registering fundamental types.
  511. The library can register its own types and templates.
  512. In the best-case scenario, if the expressions always consist of only
  513. fundamental types and library-defined types and templates, a library author
  514. can achieve the impression that the `typeof` is natively supported for her library.
  515. On the other hand, the more often expressions contain user-defined types,
  516. the more responsibility is put on the end-user, and therefore the less attractive
  517. this approach becomes.
  518. Thus, the ratio of user-defined types in the expressions should be the main
  519. factor to consider when deciding whether or not to apply the typeof facility.
  520. [endsect]
  521. [section:features Supported features]
  522. The Typeof library pre-registers fundamental types. For these types,
  523. and for any other types/templates registered by the user library or end-user,
  524. any combination of the following is supported:
  525. * Pointers;
  526. * References (except top-level);
  527. * Consts (except top-level);
  528. * Volatiles (except top-level);
  529. * Arrays;
  530. * Functions, function pointers, and references;
  531. * Pointers to member functions;
  532. * Pointers to data members.
  533. For example the following type:
  534. int& (*)(const volatile char*, double[5], void(*)(short))
  535. is supported right away, and something like:
  536. void (MyClass::*)(int MyClass::*, MyClass[10]) const
  537. is supported provided `MyClass` is registered.
  538. The Typeof Library also provides registration files for most STL classes/templates.
  539. These files are located in the std subdirectory, and named after corresponding STL headers.
  540. These files are not included by the typeof system and have to be explicitly included
  541. by the user, as needed:
  542. #include <boost/typeof/std/functional.hpp>
  543. BOOST_AUTO(fun, std::bind2nd(std::less<int>(), 21)); //create named function object for future use.
  544. [endsect]
  545. [section:what What needs to be registered?]
  546. It is possible to take advantage of the compiler when registering types for the Typeof Library.
  547. Even though there is currently no direct support for typeof in the language,
  548. the compiler is aware of what the type of an expression is, and gives an error
  549. if it encounters an expression that has not been handled correctly. In the `typeof` context,
  550. this error message will contain clues to what types needs to be registered with the
  551. Typeof Library in order for `BOOST_TYPEOF` to work.
  552. struct X {};
  553. template<typename A,bool B>
  554. struct Y {};
  555. std::pair<X,Y<int,true> > a;
  556. BOOST_AUTO(a,b);
  557. We get the following error message from VC7.1
  558. [pre
  559. error C2504: 'boost::type_of::'anonymous-namespace'::encode_type_impl<V,Type_Not_Registered_With_Typeof_System>' : base
  560. class undefined
  561. with
  562. \[
  563. V=boost::type_of::'anonymous-namespace'::encode_type_impl<boost::mpl::vector0<boost::mpl::na>,std::pair<X,Y<int,true>>>::V0,
  564. Type_Not_Registered_With_Typeof_System=X
  565. \]
  566. ]
  567. Inspecting this error message, we see that the compiler complains about `X`
  568. BOOST_TYPEOF_REGISTER_TYPE(X); //register X with the typeof system
  569. Recompiling, we get a new error message from VC7.1
  570. [pre
  571. error C2504: 'boost::type_of::'anonymous-namespace'::encode_type_impl<V,Type_Not_Registered_With_Typeof_System>' : base
  572. class undefined
  573. with
  574. \[
  575. V=boost::type_of::'anonymous-namespace'::encode_type_impl<boost::mpl::vector0<boost::mpl::na>,std::pair<X,Y<int,true>>>::V1,
  576. Type_Not_Registered_With_Typeof_System=Y<int,true>
  577. \]
  578. ]
  579. Inspecting this error message, we see that the compiler complains about `Y<int,true>`.
  580. Since `Y` is a template, and contains integral constants, we need to take more care when registering:
  581. BOOST_TYPEOF_REGISTER_TEMPLATE(Y,(typename)(bool)); //register template class Y
  582. It is a good idea to look up the exact definition of `Y` when it contains integral constants.
  583. For simple template classes containing only typenames, you can rely solely on the compiler error.
  584. The above code now compiles.
  585. This technique can be used to get an overview of which types needs to be registered
  586. for a given project in order to support `typeof`.
  587. [endsect]
  588. [section:limi Limitations]
  589. Nested template template parameters are not supported, like:
  590. template<template<template<class> class> class Tpl>
  591. class A; // can't register!
  592. Classes and templates nested inside other templates also can't be registered
  593. because of the issue of nondeduced context. This limitation is most noticeable
  594. with regards to standard iterators in Dinkumware STL, which are implemented
  595. as nested classes. Instead, instantiations can be registered:
  596. BOOST_TYPEOF_REGISTER_TYPE(std::list<int>::const_iterator)
  597. [endsect]
  598. [endsect]
  599. [section:cont Contributed By:]
  600. * Compliant compilers -- Arkadiy Vertleyb, Peder Holt
  601. * MSVC 6.5, 7.0, 7.1 -- Igor Chesnokov, Peder Holt
  602. [endsect]
  603. [section:ackn Acknowledgements]
  604. The idea of representing a type as multiple compile-time integers,
  605. and passing these integers across function boundaries using sizeof(),
  606. was taken from Steve Dewhurst's article "A Bitwise typeof Operator", CUJ 2002.
  607. This article can also be viewed online, at [@http://www.semantics.org/localarchive.html
  608. http://www.semantics.org/localarchive.html].
  609. Special thank you to Paul Mensonides, Vesa Karvonen, and Aleksey Gurtovoy
  610. for the Boost Preprocessor Library and MPL. Without these two libraries,
  611. this typeof implementation would not exist.
  612. The following people provided support, gave valuable comments,
  613. or in any other way contributed to the library development
  614. (in alphabetical order):
  615. * David Abrahams
  616. * Andrey Beliakov
  617. * Joel de Guzman
  618. * Daniel James
  619. * Vesa Karvonen
  620. * Andy Little
  621. * Paul Mensonides
  622. * Alexander Nasonov
  623. * Tobias Schwinger
  624. * Martin Wille
  625. [endsect]