Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design principles behind std::ratio<>

I was looking at the class std::ratio<> from the C++11 standard that allows to make compile-time rational arithmetic.

I found the template design and the operations implemented with classes overly complex and did not find any reason why they could not just use a more straightforward and intuitive approach by implementing a really simple rational class and defining constexpr functions for the operators. The result would have been a class easier to use and the compile-time advantages would have remained.

Does anyone have any idea of the advantages of the current std::ratio<> design compared to a simple class implementation using constexpr? Actually, I can't manage to find any advantage to the current implementation.

like image 388
Morwenn Avatar asked Sep 07 '12 21:09

Morwenn


1 Answers

When N2661 was proposed, none of the proposal authors had access to a compiler which implemented constexpr. And none of us were willing to propose something we could not build and test. So whether or not a better design could have been done with constexpr was not even part of the consideration for the design. The design was based on only those tools available to the authors at the time.

like image 92
Howard Hinnant Avatar answered Sep 25 '22 21:09

Howard Hinnant