boost::multiprecision::cpp_dec_float - Expresison template surprising behavior when using "auto const"?

272 views Asked by At

I've spent the better part of the day tracking a problem down to this example:

#include <boost/multiprecision/cpp_dec_float.hpp>

#include <iostream>

int main () 
{
  
  typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>> float_t;
  //typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>, boost::multiprecision::expression_template_option::et_off> float_t; // <--- this works ok, too!

  float_t dependency = 2.0;

  auto const val = dependency / 2;
  //float_t const val = dependency / 2; // <--- this works ok!


  std::cout << val << std::endl;
  dependency = val - 2;
  std::cout << val << std::endl;
  dependency = val - 2;
  std::cout << val << std::endl;
  dependency = val - 2;

  return 0;
}

outputs

1
-0.5
-1.25

It seems as though the dependency of val on dependency breaks the const promise.

  1. Changing the auto const to float_t const fixes the problem.

  2. Using cpp_bin_float fixes the problem

  3. On random suspicion, disabling expression templates fixes the problem

This occurs on multiple VS versions, mingw8.1, and Coliru.

Could anyone more familiar with the internal workings explain what is going on?

0

There are 0 answers