4

Can anyone advise on the correct syntax for the std::make_pair call in the std::vector::push_back call in the code below:

#include <array>
#include <vector>
#include <utility>

int main()
{
    typedef std::pair<double, double> PairType;
    std::vector<std::pair<double, std::array<PairType, 3> > > myVector;

    double Key = 0.0;
    PairType Pair1 = std::make_pair(1.0, 2.0);
    PairType Pair2 = std::make_pair(3.0, 4.0);
    PairType Pair3 = std::make_pair(5.0, 6.0);

    myVector.push_back(std::make_pair(Key, { Pair1, Pair2, Pair3 } )); // Syntax Error

    return 0;
}

The compiler (MS VS2015.2) cannot determine the type of the second argument in the std::make_pair call, which is understandable yet I don't know how to enlighten it.

pugdogfan
  • 311
  • 3
  • 10
  • Possible duplicate of [Why doesn't my template accept an initializer list](http://stackoverflow.com/questions/4757614/why-doesnt-my-template-accept-an-initializer-list) – LogicStuff Oct 12 '16 at 09:42
  • 2
    Note that you don't need make_pair: `myVector.push_back({Key, { Pair1, Pair2, Pair3 } });` – Vaughn Cato Oct 12 '16 at 14:12

2 Answers2

3

It looks like the compiler cannot figure out that { Pair1, Pair2, Pair3 } is an std::array of three pairs. Specifying the type explicitly should work:

myVector.push_back(std::make_pair(Key, std::array<PairType,3>{ Pair1, Pair2, Pair3 } ));

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

You could use std::experimental::make_array, if the compiler supports Library fundamentals v2:

using std::experimental::make_array;
myVector.push_back(std::make_pair(Key, make_array(Pair1, Pair2, Pair3) ));

LIVE from GCC

songyuanyao
  • 169,198
  • 16
  • 310
  • 405