简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM