Boost C++ Libraries: Ticket #6092: input from non integral durations makes the compiler fail https://svn.boost.org/trac10/ticket/6092 <p> The following input </p> <p> 1.0 seconds </p> <p> makes the compiler fails when trying to extract the duration </p> <p> duration&lt;double,milli&gt; d; </p> <p> cin &gt;&gt; d; </p> <pre class="wiki">../../../boost/math/common_factor_rt.hpp: In function ‘RingType boost::math::detail::gcd_euclidean(RingType, RingType) [with RingType = long double]’: ../../../boost/math/common_factor_rt.hpp:122: instantiated from ‘IntegerType boost::math::detail::gcd_integer(const IntegerType&amp;, const IntegerType&amp;) [with IntegerType = long double]’ ../../../boost/math/common_factor_rt.hpp:240: instantiated from ‘T boost::math::detail::gcd_optimal_evaluator_helper_t&lt;T, true, true&gt;::operator()(const T&amp;, const T&amp;) [with T = long double]’ ../../../boost/math/common_factor_rt.hpp:290: instantiated from ‘T boost::math::detail::gcd_optimal_evaluator&lt;T&gt;::operator()(const T&amp;, const T&amp;) [with T = long double]’ ../../../boost/math/common_factor_rt.hpp:442: instantiated from ‘T boost::math::detail::gcd_optimal(const T&amp;, const T&amp;) [with T = long double]’ ../../../boost/math/common_factor_rt.hpp:473: instantiated from ‘typename boost::math::gcd_evaluator&lt;IntegerType&gt;::result_type boost::math::gcd_evaluator&lt;IntegerType&gt;::operator()(const IntegerType&amp;, const IntegerType&amp;) const [with IntegerType = long double]’ ../../../boost/math/common_factor_rt.hpp:505: instantiated from ‘IntegerType boost::math::gcd(const IntegerType&amp;, const IntegerType&amp;) [with IntegerType = long double]’ ../../../boost/chrono/io/duration_get.hpp:239: instantiated from ‘InputIterator boost::chrono::duration_get&lt;CharT, InputIterator&gt;::get(InputIterator, InputIterator, std::ios_base&amp;, std::_Ios_Iostate&amp;, boost::chrono::duration&lt;Rep2, Period2&gt;&amp;, const CharT*, const CharT*) const [with Rep = double, Period = boost::ratio&lt;1l, 1l&gt;, CharT = char, InputIterator = std::istreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;]’ ../../../boost/chrono/io/duration_get.hpp:294: instantiated from ‘InputIterator boost::chrono::duration_get&lt;CharT, InputIterator&gt;::get(InputIterator, InputIterator, std::ios_base&amp;, std::_Ios_Iostate&amp;, boost::chrono::duration&lt;Rep2, Period2&gt;&amp;) const [with Rep = double, Period = boost::ratio&lt;1l, 1l&gt;, CharT = char, InputIterator = std::istreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;]’ ../../../boost/chrono/io/duration_io.hpp:593: instantiated from ‘std::basic_istream&lt;_CharT, _Traits&gt;&amp; boost::chrono::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;&amp;, boost::chrono::duration&lt;Rep2, Period2&gt;&amp;) [with CharT = char, Traits = std::char_traits&lt;char&gt;, Rep = double, Period = boost::ratio&lt;1l, 1l&gt;]’ io/duration_input.cpp:15: instantiated from ‘void test_good(const char*, D) [with D = boost::chrono::duration&lt;double, boost::ratio&lt;1l, 1l&gt; &gt;]’ io/duration_input.cpp:52: instantiated from here ../../../boost/math/common_factor_rt.hpp:102: error: invalid operands of types ‘long double’ and ‘long double’ to binary ‘operator%’ ../../../boost/math/common_factor_rt.hpp:102: error: in evaluation of ‘operator%=(long double, long double)’ ../../../boost/math/common_factor_rt.hpp:106: error: invalid operands of types ‘long double’ and ‘long double’ to binary ‘operator%’ ../../../boost/math/common_factor_rt.hpp:106: error: in evaluation of ‘operator%=(long double, long double)’ </pre><p> This is because the common type is a double and gcd doesn't works for it. </p> <pre class="wiki">// if (is_integral&lt;intermediate_type&gt;::value) // { // // Reduce r * num / den // common_type_t t = math::gcd&lt;common_type_t&gt;(r, den); // r /= t; // den /= t; // if (den != 1) // { // // Conversion to Period is integral and not exact // is.setstate(is.failbit); // return is; // } // } </pre><p> This code could be replaced by a template to avoid the bad code to be instantiated. </p> <pre class="wiki">template &lt;typename intermediate_type&gt; typename enable_if&lt;is_integral&lt;intermediate_type&gt;, bool&gt;::type reduce(intermediate_type&amp; r, unsigned long long&amp; den, std::ios_base::iostate&amp; err) { typedef typename common_type&lt;intermediate_type, unsigned long long&gt;::type common_type_t; // Reduce r * num / den common_type_t t = math::gcd&lt;common_type_t&gt;(common_type_t(r), common_type_t(den)); r /= t; den /= t; if (den != 1) { // Conversion to Period is integral and not exact err |= std::ios_base::failbit; return false; } return true; } template &lt;typename intermediate_type&gt; typename disable_if&lt;is_integral&lt;intermediate_type&gt;, bool&gt;::type reduce(intermediate_type&amp; , unsigned long long&amp; , std::ios_base::iostate&amp; ) { return true; } </pre><p> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6092 Trac 1.4.3 viboes Thu, 03 Nov 2011 23:15:12 GMT status changed https://svn.boost.org/trac10/ticket/6092#comment:1 https://svn.boost.org/trac10/ticket/6092#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> Fixed in trunk <a class="changeset" href="https://svn.boost.org/trac10/changeset/75237" title="Chrono: fix 2 new tests [N/D]second and duration input from float or double">r75237</a>. </p> Ticket viboes Thu, 10 Nov 2011 21:59:55 GMT milestone changed https://svn.boost.org/trac10/ticket/6092#comment:2 https://svn.boost.org/trac10/ticket/6092#comment:2 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.49.0</span> </li> </ul> Ticket viboes Sat, 26 Nov 2011 00:11:14 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/6092#comment:3 https://svn.boost.org/trac10/ticket/6092#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/75665" title="Chrono: Fix #6092,#6093,#6113">[75665]</a>) Chrono: Fix <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6092" title="#6092: Bugs: input from non integral durations makes the compiler fail (closed: fixed)">#6092</a>,<a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6093" title="#6093: Bugs: [1/3]second fails as valid duration input (closed: fixed)">#6093</a>,<a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6113" title="#6113: Bugs: duplicate symbol when BOOST_CHRONO_HEADER_ONLY is defined (closed: fixed)">#6113</a> </p> Ticket