I study the ratio in C ++ 11. According to cplusplus.com , and the book is Professional C ++ 2nd (the next paragraph is an excerpt from it).
The numerator and denominator of a rational number are represented as constants of compilation time of the type std::intmax_t . Due to the compilation of the temporary nature of these rational numbers, their use may look a little complicated and different from the usual. You cannot define a relationship object in the same way you define normal objects, and you cannot call methods on a topic. You need to use typedefs .
That means I have to write
typedef ratio<1,3> one_third;
instead
ratio<1,3> one_third;
But I find that these two ways of recording ratio work. And I can access the members of the relationship using either . , or :: .
Question 1. Are the books cplusplus.com and Professional C ++ wrong?
The following snippet is from cplusplus.com example.
typedef std::ratio<1,3> one_third; typedef std::ratio<2,4> two_fourths; typedef std::ratio_add<one_third,two_fourths> sum; std::cout << sum::den << std::endl;
Question 2. However, I got an error (since VS 2012)
error C2039: 'den' : is not a member of 'std::ratio_add<_R1,_R2>'
According to the comments, using typedef ratio_add<one_third, two_fourths>::type sum more portable.
source share