Changes between Version 20 and Version 21 of BestPracticeHandbook
- Timestamp:
- May 11, 2015, 10:52:33 PM (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
BestPracticeHandbook
v20 v21 788 788 }}} 789 789 790 This works very well when your policy implementations fit nicely into template types, however for when they don't an ADL based namespace composure design pattern can be highly effective. Here is the same program written using namespace composure: 790 This works very well when your policy implementations fit nicely into template types, and when the number of policy taking template types is reasonably low (otherwise you'll be doing a lot of typing as any changes to the policy design requires modifying every single instantiation of the policy taking template types). Another problem with policy based design is that it generates a lot of template instantiations, and generating a lot of template instantiations is bad because compilers often cannot do constant time type lookups and instead have linear or worse type lookups, so instantiating tens of million types is always going to be a lot slower to compile and sometimes link than millions of types. 791 792 Consider instead doing an ADL based namespace composure design pattern which although just a different way of doing policy based design, can be highly effective in those niches where the traditional policy taking template approach falls down. Here is the same program above written using ADL namespace composure: 791 793 792 794 {{{#!c++ … … 803 805 template<class T> void run(T v) 804 806 { 805 print(message(v)); // Can 't instantiate message nor printuntil T is known807 print(message(v)); // Cannot instantiate message() nor print() until T is known 806 808 } 807 809 } … … 860 862 }}} 861 863 862 The above pattern is in fact entirely C++ 03 code and uses no C++ 11. However, template aliasing in C++ 11 makes the above pattern much more flexible. Have a look at https://github.com/ptal/expected/blob/master/include/boost/functional/monads/rebindable.hpp for examples of this ADL invoked namespace composure design pattern 864 The first example instantiates five types to be considered during global type lookup, whereas the second example instantiates no types at all at global lookup scope. The second example also requires no modification of each instantiation of the HelloWorld policy taking implementation in most cases of refactoring e.g. adding a third policy parameter 865 866 The above pattern is in fact entirely C++ 03 code and uses no C++ 11. However, template aliasing in C++ 11 makes the above pattern much more flexible. Have a look at https://github.com/ptal/expected/blob/master/include/boost/functional/monads/rebindable.hpp for examples of this ADL invoked namespace composure design pattern. 863 867 864 868 == 16. BUILD: Consider defaulting to header only, but make available C++ 11 features very substantially reducing build times ==