- ratio[meta header]
- std[meta namespace]
- class template[meta id-type]
- cpp11[meta cpp]
namespace std {
template <intmax_t N, intmax_t D = 1>
class ratio {
public:
using type = ratio<num, den>;
static constexpr intmax_t num;
static constexpr intmax_t den;
};
}- intmax_t[link /reference/cstdint/intmax_t.md]
ratioは、コンパイル時に有理数を表現するためのクラスである。メンバ定数として、num(分子:numerator)とden(分母:denominator)を持つ。
numとdenの値は、gcd(最大公約数:the greatest common divisor)を使用して、以下のようにNとDを約分して割り当てる:
denの値が0でもなく、intmax_tの最小値でもないこと。
#include <ratio>
int main()
{
using r = std::ratio<3, 12>;
static_assert(r::num == 1, "num is 1");
static_assert(r::den == 4, "den is 4");
}- std::ratio[color ff0000]
- C++11
- Clang: 3.0 [mark verified]
- GCC: 4.4.7 [mark verified]
- Visual C++: ??
- LWG Issue 921. Rational Arithmetic should use template aliases
- C++11で、
ratioの算術演算がクラステンプレートからエイリアステンプレートへ改められ、ratioにネストされた型typeが追加された。演算結果が既約化されたratioそのものとして扱えるようにするため
- C++11で、
- LWG Issue 1195. "Diagnostic required" wording is insufficient to prevent UB
- C++11で、テンプレート引数が要件を満たさない場合の扱いが、診断が必要な要件違反から、プログラムが不適格となる規定へ改められた
- LWG Issue 1388. LWG 1281 incorrectly accepted
- C++11で、ネストされた型
typeがratio<num, den>として定義されることが規定された
- C++11で、ネストされた型