Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Conceptual Archetypes for Reals and Distributions

There are a few concept archetypes available:

Real concept

std_real_concept is an archetype for theReal types, including the built-in float, double, long double.

#include <boost/concepts/std_real_concept.hpp>
namespace boost{
namespace math{
namespace concepts
{
  class std_real_concept;
}
}} // namespaces

The main purpose in providing this type is to verify that standard library functions are found via a using declaration - bringing those functions into the current scope - and not just because they happen to be in global scope.

In order to ensure that a call to say pow can be found either via argument dependent lookup, or failing that then in the std namespace: all calls to standard library functions are unqualified, with the std:: versions found via a using declaration to make them visible in the current scope. Unfortunately it's all to easy to forget the using declaration, and call the double version of the function that happens to be in the global scope by mistake.

For example if the code calls ::pow rather than std::pow, the code will cleanly compile, but truncation of long doubles to double will cause a significant loss of precision. In contrast a template instantiated with std_real_concept will only compile if the all the standard library functions used have been brought into the current scope with a using declaration.

Testing the real concept

There is a test program libs/math/test/std_real_concept_check.cpp that instantiates every template in this library with type std_real_concept to verify its usage of standard library functions.

#include <boost/math/concepts/real_concept.hpp>
namespace boost{
namespace math{
namespace concepts{

class real_concept;

}}} // namespaces

real_concept is an archetype for user defined real types, it declares its standard library functions in its own namespace: these will only be found if they are called unqualified allowing argument dependent lookup to locate them. In addition this type is useable at runtime: this allows code that would not otherwise be exercised by the built-in floating point types to be tested. There is no std::numeric_limits<> support for this type, since numeric_limits is not a conceptual requirement for RealTypes.

NTL RR is an example of a type meeting the requirements that this type models, but note that use of a thin wrapper class is required: refer to "Using With NTL - a High-Precision Floating-Point Library".

There is no specific test case for type real_concept, instead, since this type is usable at runtime, each individual test case as well as testing float, double and long double, also tests real_concept.

Distribution Concept

Distribution Concept models statistical distributions.

#include <boost/math/concepts/distribution.hpp>
namespace boost{
namespace math{
namespace concepts
{
  template <class RealType>
  class distribution_archetype;

  template <class Distribution>
  struct DistributionConcept;

}}} // namespaces

The class template distribution_archetype is a model of the Distribution concept.

The class template DistributionConcept is a concept checking class for distribution types.

Testing the distribution concept

The test program distribution_concept_check.cpp is responsible for using DistributionConcept to verify that all the distributions in this library conform to the Distribution concept.

The class template DistributionConcept verifies the existence (but not proper function) of the non-member accessors required by the Distribution concept. These are checked by calls like

v = pdf(dist, x); // (Result v is ignored).

And in addition, those that accept two arguments do the right thing when the arguments are of different types (the result type is always the same as the distribution's value_type). (This is implemented by some additional forwarding-functions in derived_accessors.hpp, so that there is no need for any code changes. Likewise boilerplate versions of the hazard/chf/coefficient_of_variation functions are implemented in there too.)


PrevUpHomeNext