...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
detail/ct_gcd_lcm.hpp provides two compile-time algorithms: greatest common divisor and least common multiple.
namespace details { namespace pool { template <unsigned A, unsigned B> struct ct_gcd { static const unsigned value = ...; }; template <unsigned A, unsigned B> struct ct_lcm { static const unsigned value = ...; }; } // namespace pool } // namespace details
Symbol | Meaning |
---|---|
A, B | compile-time unsigned integer constants[5.19/1] |
Expression | Result Type | Value | Precondition |
---|---|---|---|
ct_gcd<A, B>::value | compile-time unsigned integer constant | The greatest common divisor of A and B | A != 0 && B != 0 |
ct_lcm<A, B>::value | compile-time unsigned integer constant | The least common multiple of A and B | A != 0 && B != 0 |
Since these are compile-time algorithms, violation of the preconditions will result in a compile-time error.
5.19/1: Expressions: Constant Expressions: ". . . An integral constant expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types. Only type conversions to integral or enumeration types can be used. In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used."
This header may be replaced by a Boost compile-time algorithms library.
Revised 05 December, 2006
Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)
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)