Initialize unordered_map in initializer list

I am trying to find a solution to what could be a very trivial problem. I would like to initialize mine const unordered_map

in the class initializer list. However, I have not yet found the syntax that the compiler will accept (GCC 6.2.0). Code link here .

#include <unordered_map>

class test {
 public:
    test()
      : map_({23, 1345}, {43, -8745}) {}

 private:
   const std::unordered_map<long, long> map_;
 };

      

Mistake:

main.cpp: In constructor 'test::test()':
main.cpp:6:36: error: no matching function for call to 'std::unordered_map<long int, long int>::unordered_map(<brace-enclosed initializer list>, <brace-enclosed initializer list>)'
  : map_({23, 1345}, {43, -8745}) {}
                                ^

      

Are complex constants not allowed for initialization in the initializer list? Or should the syntax be different?

+3


source to share


1 answer


Use parentheses instead of parentheses



class test {
 public:
    test()
      : map_{{23, 1345}, {43, -8745}} {}

 private:
   const std::unordered_map<long, long> map_;
 };

      

+4


source







All Articles