Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cannot call lambda expression from map

Tags:

c++

c++11

I have created a class for temperature conversions and I've decided to create a conversion table with a structure like this:

temperature | conversion formula

For this reason, I've created a std::map that has the key as temperature index and then the formula. Here you can see the implementation:

enum class TempType {Celsius, Fahrenheit, Kelvin, Rankine, Delisle, Newton, Reaumur, Romer};

class Temperature {
private:

  double value;   //value of the temperature
  TempType kind;  //Celsius, Fahrenheit...

  //conversion tables
  std::map<TempType, std::function<double(double)>> fromCelsius = {
      { TempType::Fahrenheit, [](double x) { return x * (9/5) + 32; } },
      { TempType::Kelvin, [](double x) { return x + 273.15; } },
      { TempType::Rankine, [](double x) { return (x + 273.15) * (9/5); } },
      { TempType::Delisle, [](double x) { return (100 - x) * (3/2); } },
      { TempType::Reaumur, [](double x) { return x * (33/100); } },
      { TempType::Newton, [](double x) { return x * (4/5); } },
      { TempType::Romer, [](double x) { return x * (21/40) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromFahrenheit = {
      { TempType::Celsius, [](double x) { return (x - 32) * (5/9); } },
      { TempType::Kelvin, [](double x) { return (x + 459.67) * (5/9); } },
      { TempType::Rankine, [](double x) { return x + 459.67; } },
      { TempType::Delisle, [](double x) { return (212 - x) * (5/6); } },
      { TempType::Newton, [](double x) { return (x - 32) * (11/60); } },
      { TempType::Reaumur, [](double x) { return (x - 32) * (4/9); } },
      { TempType::Romer, [](double x) { return (x - 32) * (7/24) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromKelvin = {
      { TempType::Celsius, [](double x) { return x - 273.15; } },
      { TempType::Fahrenheit, [](double x) { return x * (9/5) - 459.67; } },
      { TempType::Rankine, [](double x) { return x * (9/5); } },
      { TempType::Delisle, [](double x) { return (373.15 - x) * (3/2); } },
      { TempType::Newton, [](double x) { return (x - 273.15) * (33/100); } },
      { TempType::Reaumur, [](double x) { return (x - 273.15) * (4/5); } },
      { TempType::Romer, [](double x) { return (x - 273.15) * (21/40) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromRankine = {
      { TempType::Celsius, [](double x) { return (x - 491.67) * (5/9); } },
      { TempType::Fahrenheit, [](double x) { return x - 459.67; } },
      { TempType::Kelvin, [](double x) { return x * (5/9); } },
      { TempType::Delisle, [](double x) { return (671.67 - x) * (5/6); } },
      { TempType::Newton, [](double x) { return (x - 491.67) * (11/60); } },
      { TempType::Reaumur, [](double x) { return (x - 491.67) * (4/9); } },
      { TempType::Romer, [](double x) { return (x - 491.67) * (7/24) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromDelisle = {
      { TempType::Celsius, [](double x) { return 100 - x * (5/9); } },
      { TempType::Fahrenheit, [](double x) { return 212 - x * (6/5); } },
      { TempType::Kelvin, [](double x) { return 373.15 - x * (2/3); } },
      { TempType::Rankine, [](double x) { return 671.67 - x * (6/5); } },
      { TempType::Newton, [](double x) { return 33 - x * (11/50); } },
      { TempType::Reaumur, [](double x) { return 80 - x * (8/15); } },
      { TempType::Romer, [](double x) { return 60 - x * (7/20); } }
  };

  std::map<TempType, std::function<double(double)>> fromNewton = {
      { TempType::Celsius, [](double x) { return x * (100/33); } },
      { TempType::Fahrenheit, [](double x) { return x * (60/11) + 32; } },
      { TempType::Kelvin, [](double x) { return x * (100/33) + 273.15; } },
      { TempType::Rankine, [](double x) { return x * (60/11) + 491.67; } },
      { TempType::Delisle, [](double x) { return (33 - x) * (50/11); } },
      { TempType::Reaumur, [](double x) { return x * (80/33); } },
      { TempType::Romer, [](double x) { return x * (35/22) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromReamur = {
      { TempType::Celsius, [](double x) { return x * (5/4); } },
      { TempType::Fahrenheit, [](double x) { return x * (9/4) + 32; } },
      { TempType::Kelvin, [](double x) { return x * (5/4) + 273.15; } },
      { TempType::Rankine, [](double x) { return x * (9/4) + 491.67; } },
      { TempType::Delisle, [](double x) { return (80 - x) * (15/8); } },
      { TempType::Newton, [](double x) { return x * (33/80); } },
      { TempType::Romer, [](double x) { return x * (21/32) + 7.5; } }
  };

  std::map<TempType, std::function<double(double)>> fromRomer = {
      { TempType::Celsius, [](double x) { return (x - 7.5) * (40/21); } },
      { TempType::Fahrenheit, [](double x) { return (x - 7.5) * (24/7) + 32; } },
      { TempType::Kelvin, [](double x) { return (x - 7.5) * (40/21) + 273.15; } },
      { TempType::Rankine, [](double x) { return (x - 7.5) * (24/7) + 491.67; } },
      { TempType::Delisle, [](double x) { return (60 - x) * (20/7); } },
      { TempType::Newton, [](double x) { return (x - 7.5) * (22/35); } },
      { TempType::Reaumur, [](double x) { return (x - 7.5) * (32/21); } }
  };

public:
}    
#endif // TEMPERATURE_H

Now, if I want to convert from a temperature object to another, I use this code:

Temperature x(20, TempType::Celsius); //20 °C
double s = x.convertTo(TempType::Fahrenheit); //convert 20 °c to 68 °F

The conversion code is this:

double Temperature::convertTo(const TempType& temperature) const {

  if (temperature == kind) {
    return value;
  }

  double result = -1;

  switch (temperature) {
    case TempType::Celsius:
      result = fromCelsius.at(kind)(value);
      break;
    case TempType::Fahrenheit:
      result = fromFahrenheit.at(kind)(value);
      break;
    case TempType::Kelvin:
      result = fromKelvin.at(kind)(value);
      break;
    case TempType::Rankine:
      result = fromRankine.at(kind)(value);
      break;
    case TempType::Delisle:
      result = fromDelisle.at(kind)(value);
      break;
    case TempType::Newton:
      result = fromNewton.at(kind)(value);
      break;
    case TempType::Reaumur:
      result = fromReamur.at(kind)(value);
      break;
    case TempType::Romer:
      result = fromRomer.at(kind)(value);
      break;
    default:
      break;
    }

  return result;

}

I use the key of the map (a TempType) and then I pass to the lambda the parameter. The result returned is always 0. Any idea?

I am using QTCreator and mingw.

like image 778
Raffaele Rossi Avatar asked Feb 21 '18 18:02

Raffaele Rossi


1 Answers

In C++ 4/5, 21/40, 5/6 and the like are all zero because the division is performed in integer arithmetic.

You need to write 4./5 instead.

like image 128
6502 Avatar answered Oct 06 '22 00:10

6502