cpp<int C, int S, int I, int L, int LL>
Description This policy is used to promote safe types in arithmetic expressions according to the rules in the C++ standard. But rather than using the native C++ standard types supported by the compiler, it uses types whose length in number of bits is specified by the template parameters. This policy is useful for running test programs which use C++ portable integer types but which are destined to run on an architecture which is different than the one on which the test program is being built and run. This can happen when developing code for embedded systems. Algorithms developed or borrowed from one architecture but destined for another can be tested on the desktop. Note that this policy is only applicable to safe types whose base type is a type fulfilling the type requirements of Integer.
Template Parameters Parameter Type Description C int Number of bits in a char S int Number of bits in a short I int Number of bits in an integer L int Number of bits in a long LL int Number of bits in a long long
Model of PromotionPolicy
Example of Use Consider the following problem. One is developing software which uses a very small microprocessor and a very limited C compiler. The chip is so small, you can't print anything from the code, log, debug or anything else. One debugs this code by using the "burn" and "crash" method - you burn the chip (download the code), run the code, observe the results, make changes and try again. This is a crude method which is usually the one used. But it can be quite time consuming. Consider an alternative. Build and compile your code in testable modules. For each module write a test which exercises all the code and makes it work. Finally download your code into the chip and - voilĂ  - working product. This sounds great, but there's one problem. Our target processor - in this case a PIC162550 from Microchip Technology is only an 8 bit CPU. The compiler we use defines INT as 8 bits. This (and a few other problems), make our algorithm testing environment differ from our target environment. We can address this by defining INT as a safe integer with a range of 8 bits. By using a custom promotion policy, we can force the evaluation of C++ expressions in the test environment to be the same as that in the target environment. Also in our target environment, we can trap any overflows or other errors. So we can write and test our code on our desktop system and download the code to the target knowing that it just has to work. This is a huge time saver and confidence builder. The following code is taken from a real project which has used this method. #include "../include/safe_integer.hpp" #include "../include/cpp.hpp" ////////////////////////////////////////////////////////////// // Stepper Motor Control // emululate evironment for pic162550 // data widths used by the CCS compiler for pic 16xxx series using pic16_promotion = boost::numeric::cpp< 8, // char 8, // short - not used by pic 16xxxx 8, // int 16, // long 32 // long long >; template <typename T> // T is char, int, etc data type using safe_t = boost::numeric::safe< T, pic16_promotion, boost::numeric::loose_trap_policy // use for compiling and running tests >; using int8 = safe_t<std::int8_t>; using int16 = safe_t<std::int16_t>; using int32 = safe_t<std::int32_t>; using uint8 = safe_t<std::uint8_t>; using uint16 = safe_t<std::uint16_t>; using uint32 = safe_t<std::uint32_t>; //////////////////////////////////////////////////////////////// // Mock defines, functions etc which are in the "real application" ... // return value in steps /* Use the formula: stopping dist = v **2 / a / 2 */ uint16 get_stopping_distance(LEMPARAMETER velocity){ int32 d; d = velocity; d *= velocity; d /= lem.acceleration; d /= 2; return d; } ... Note the usage of the compile time trap policy in order to detect at compile time any possible error conditions. As I write this, this is still being refined. Hopefully this will be available by the time you read this.
Header #include <boost/numeric/safe_numerics/cpp.hpp>