How efficient is emplace_back (pair)?

I have

using namespace std;   // for convenience in SO question only
vector<pair<array<int,3>, int>> foo;

and want the emplace_backitem with pair::firstwhile holding {i,j,k}and pair::secondholding q. The only way to get this compilation was with a rather awkward

foo.emplace_back(piecewise_construct,
                 forward_as_tuple(i,j,k),
                 forward_as_tuple(q));

Is it effective (i.e. guaranteed to tuplebe optimized)? Or is there another way that is guaranteed to be effective?

(I tried

foo.emplace_back(std::initializer_list<int>{i,j,k}, q);

but to no avail with gcc 4.8.1). I know that I can avoid this problem by specifying

struct element : std::pair<std::array<int,3>,int>
{
  element(int i, int j, int k, int q)
  { first={i,j,k}; second=q; }
};

vector<element> foo;
foo.emplace_back(i,j,k,q);

but I would rather do without such extra code.

+4
source share
1 answer

std::array<T, N> , a std::initializer_list<U> U, U = T. , -init- .

.

foo.emplace_back(std::array<int,3>{i,j,k}, q);

, , . , , emplace_back . , , .

+3

All Articles