rationale.qbk

来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 100 行

QBK
100
字号
[/ / Copyright (c) 2008 Eric Niebler / / Distributed under the Boost Software License, Version 1.0. (See accompanying / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) /][section:rationale Appendix B: Rationale][/==================================================][section:static_initialization Static Initialization][/==================================================]Proto expression types are PODs (Plain Old Data), and do not have constructors.They are brace-initialized, as follows:    terminal<int>::type const _i = {1};The reason is so that expression objects like `_i` above can be ['staticallyinitialized]. Why is static initialization important? The terminals of manydomain-specific embedded languages are likely to be global const objects, like`_1` and `_2` from the Boost Lambda Library. Were these object to requirerun-time initialization, it might be possible to use these objects before theyare initialized. That would be bad. Statically initialized objects cannot bemisused that way.[endsect][/======================================================================][section:result_of Proto Transforms and the Restricted ResultOf Protocol][/======================================================================]All Proto primitive transforms make use of a variant of the TR1 ResultOfprotocol for computing the type of the transform's return value. Suchtransforms must have a nested `result<>` template (not a nested `result_type`typedef) which takes exactly three parameter types. That is, it must bedefined as:    template<typename Sig> struct result {};        template<typename This, typename Expr, typename State, typename Visitor>    struct result<This(Expr, State, Visitor)>    {        typedef ... type;    };In the above, `Expr`, `State`, and `Visitor` are assumed by Proto to benon-cv-qualified non-reference types. The implication is that for someProto transform `Tr`, some `result_of<>` instantiations work while othersdon't. See below.    // ERROR, doesn't work:    boost::result_of<Tr(Expr const &, State const &, Visitor &)>::type        // OK, works:    boost::result_of<Tr(Expr, State, Visitor)>::typeIt is done this way largely for compile-time performance. Fullcompliance with the TR1 ResultOf protocol incurs a not insignificant penaltyat compile time. Metaprogramming tricks are needed to first detect a nested`result_type` typedef if it exists. And each nested `result<>` templatewould need to be coded specially to handle references and cv-qualifiers, whichincurs many instantiations of `remove_reference<>` and `remove_cv<>`. In private testing, this was found to have a measurable impact on compile-time performance in the order of 10-15%, which was deemed unacceptable.The restricted protocol improves compile times while remaining largelycompatible with TR1's `result_of<>`. As a side benefit, it makesProto's primitive transforms easier to implement, since the user need notworry about stripping references and cv-qualification in their nested`result<>` templates.[endsect][/=========================================================][section:preprocessor Why Not Reuse MPL, Fusion, et cetera?][/=========================================================]Anyone who has peeked at Proto's source code has probably wondered,"Why all the dirty preprocessor gunk? Couldn't this have been allimplemented cleanly on top of libraries like MPL and Fusion?" The answer is that Proto could have been implemented this way, and in factwas at one point. The problem is that template metaprogramming (TMP)makes for very long compile times. As a foundation upon which other TMP-heavy libraries will be built, Proto itself should be as lightweightas possible. That is achieved by prefering preprocessor metaprogrammingto template metaprogramming. Expanding a macro is far more efficientthan instantiating a template. In some cases, the "clean" version takes10x longer to compile than the "dirty" version.The "clean and slow" version of Proto can still be found athttp://svn.boost.org/svn/boost/branches/proto/v3. Anyone who is interestedcan download it and verify that it is, in fact, unusably slow to compile.Note that this branch's development was abandoned, and it does notconform exactly with Proto's current interface.[endsect][endsect]

⌨️ 快捷键说明

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