0

Consider following example:

priority_queue<int[3], vector<int[3]>, greater<int[3]>> pq;
pq.push({{2,3,4}}));
int * x=pq.top();

It says that no matching function found for push().

Of course, we can use a tuple to do the same thing. But I'm just curious can we use array directly.

2
  • Why people use int[3] in c++? Commented Mar 1, 2020 at 9:44
  • Use std::array. Commented Mar 1, 2020 at 9:50

1 Answer 1

4

You can't do std::vector<int[3]>.

std::vector<T> requires T to be Erasable, which means that the expression

allocator_traits<A>::destroy(m, p)

is well formed. When A = std::allocator<int[3]>, this tries to do

p->~T()

which is equivalent to

(*p).~T()

where p is of type int(*)[3] and T is int[3]. Arrays are not allowed to appear in a pseudo destructor call: [expr.pseudo]/2

The left-hand side of the dot operator shall be of scalar type. [...]

(Arrays are not scalar types.)


You can use std::array<int, 3> though. It is compared in lexicographical order by default.

Sign up to request clarification or add additional context in comments.

3 Comments

would it be possible to write a custom allocator such that c arrays are Erasable (with that allocator) ?
@463035818_is_not_a_number I guess we can, in principle. It would be a pain to use though - we can't resize the vector, for example, as arrays are still not MoveInsertible.
yeah I was expecting it to be pain. It was just curiosity. Came here from a almost dupe and my answer says just "not possible", now I wasnt sure if that is right, because Erasable merely requires allocator_traits<A>::destroy(m, p) not more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.