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.

kilasuelika
  • 165
  • 6

1 Answers1

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.

L. F.
  • 19,445
  • 8
  • 48
  • 82
  • would it be possible to write a custom allocator such that c arrays are Erasable (with that allocator) ? – 463035818_is_not_an_ai May 28 '21 at 08:25
  • 1
    @463035818_is_not_a_number I guess [we can](https://wandbox.org/permlink/mOI6NfjuaNg8PUr0), in principle. It would be a pain to use though - we can't resize the vector, for example, as arrays are still not MoveInsertible. – L. F. May 28 '21 at 09:36
  • 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::destroy(m, p)` not more – 463035818_is_not_an_ai May 28 '21 at 09:39