Changes between Version 20 and Version 21 of BestPracticeHandbook


Ignore:
Timestamp:
May 11, 2015, 10:52:33 PM (7 years ago)
Author:
Niall Douglas
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • BestPracticeHandbook

    v20 v21  
    788788}}}
    789789
    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:
     790This 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
     792Consider 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:
    791793
    792794{{{#!c++
     
    803805  template<class T> void run(T v)
    804806  {
    805     print(message(v));  // Can't instantiate message nor print until T is known
     807    print(message(v));  // Cannot instantiate message() nor print() until T is known
    806808  }
    807809}
     
    860862}}}
    861863
    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
     864The 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
     866The 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.
    863867
    864868== 16. BUILD: Consider defaulting to header only, but make available C++ 11 features very substantially reducing build times ==