Initialization std :: pair <double, std :: array <std :: pair <double, double>, 3 >>

Can anyone advise the correct syntax for calling std :: make_pair in a call to std :: vector :: push_back 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 call to std :: make_pair, which is understandable, but I do not know how to enlighten it.

+4
source share
2 answers

It seems that the compiler can not figure out what { Pair1, Pair2, Pair3 }is std::arrayof three pairs. The type declaration should obviously work:

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

Demo version

+3
source

std:: experimental:: make_array, v2:

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

LIVE from GCC

+3

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


All Articles