How to use make_pair to create id and struct (object) pairs?

I tried to create a pair of id and object as follows:

 #include <iostream>
  #include <utility>
  #include <functional>

  struct num{

      double x;
      double y;

  };


  int main(){

      auto tmp = std::make_pair(1, {1.0, 2.0});

 }

I get an error like error: no matching function for call to 'make_pair(int, <brace-enclosed initializer list>)'

Is there a way to create a pair of id and object?

+4
source share
1 answer

No. Here's how you should create your pair:

auto tmp = std::make_pair(1, num{1.0, 2.0});

Or alternatively (as @StoryTeller mentioned):

std::pair<int,num> tmp {1, {1.0, 2.0}};

Now, in both cases, the compiler has a clue to what the {1.0, 2.0}initializer means for num.

+6
source

Source: https://habr.com/ru/post/1664478/


All Articles