0

I am getting the following error:

braced initialization of a variable declared with a placeholder type but without `=` requires exactly one element inside the braces
  const auto temperature_units{
    {"K", [] (double x)->double {return x;}},
    {"C", [] (double x)->double {return x+273.15;}},
    {"\u2103",  [] (double x)->double {return x+273.15;}}, // degree celcius symbol
    {"\u00B0C", [] (double x)->double {return x+273.15;}}, // degree symbol plus C
    {"F",  [] (double x)->double {return (x+459.67)*5.0/9.0;}},
    {"\u2109", [] (double x)->double {return (x+459.67)*5.0/9.0;}},// degree fahrenheit symbol
    {"\u00B0F", [] (double x)->double {return (x+459.67)*5.0/9.0;}}
  };

Something like

  const std::map<std::string, auto> temperature_units{
    {"K", [] (double x)->double {return x;}},
    {"C", [] (double x)->double {return x+273.15;}},
    {"\u2103",  [] (double x)->double {return x+273.15;}}, // degree celcius symbol
    {"\u00B0C", [] (double x)->double {return x+273.15;}}, // degree symbol plus C
    {"F",  [] (double x)->double {return (x+459.67)*5.0/9.0;}},
    {"\u2109", [] (double x)->double {return (x+459.67)*5.0/9.0;}},// degree fahrenheit symbol
    {"\u00B0F", [] (double x)->double {return (x+459.67)*5.0/9.0;}}
  };

also does not work as that is error: invalid use of ‘auto apparently. I am not sure how else to write this, as apparently lambdas do not have a defined type

6
  • 4
    Lambdas may not have a defined type, but they are castable to std::function<double, double> in your case. Use that as type in your map. Commented Mar 11, 2021 at 20:24
  • @Blindy thanks that worked in this case - is there maybe also a more general answer? I guess you rarely would ever define a map inside a block where you might want to capture something Commented Mar 11, 2021 at 20:26
  • 1
    That is the general answer, even capturing lambdas can be cast to std::function<>. This will work in every single case, and perhaps irrelevant to this question, but this same thing works in C# (only it's Action<> or Func<>) and I use it at least a couple of times in every project. Commented Mar 11, 2021 at 20:28
  • 2
    If you never plan to use capturing lambda you may use pointer to function, but if signature of lambda may not exactly match or you need to use capturing one then there is no other option than std::function I am afraid. Commented Mar 11, 2021 at 20:37
  • std::map<std::string, std::function<double(double)>>. godbolt.org/z/644Phj Commented Mar 11, 2021 at 20:48

1 Answer 1

1
using namespace std::literals;


    const std::map temperature_units {
        std::make_pair("K"s, std::function<double(double)>{[] (double x)->double {return x;}}),
        {"C"s, [] (double x)->double {return x+273.15;}},
        {"\u2103"s,  [] (double x)->double {return x+273.15;}}, // degree celcius symbol
        {"\u00B0C"s, [] (double x)->double {return x+273.15;}}, // degree symbol plus C
        {"F"s,  [] (double x)->double {return (x+459.67)*5.0/9.0;}},
        {"\u2109"s, [] (double x)->double {return (x+459.67)*5.0/9.0;}},// degree fahrenheit symbol
        {"\u00B0F"s, [] (double x)->double {return (x+459.67)*5.0/9.0;}}
    };

https://godbolt.org/z/M5er48 some variation https://godbolt.org/z/ozqG56

So basically problem is to enforce proper type of initialization list.

Sign up to request clarification or add additional context in comments.

2 Comments

I do not see how it is any better than declaring std::map with proper type. I can easily see how it would be worse.
I think this was OP desire to auto deduce parameters types of map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.