I have a number of situations where queues are being used, which can grow to sizes in the many hundreds. Unfortunately, there is no one-call-solution to emptying the queue, should it become necessary. I'm wondering if using a scoped queue to do a swap, and then let the scoped queue be destroyed, breaks any memory allocation/management rules?
The following snippet is an example of what I am proposing. Seems to work, unsure of results if used many times over long periods.
#include <cstdlib>
#include <iostream>
#include <queue>
int
main()
{
std::queue<int> foo;
foo.push(10);
foo.push(20);
foo.push(30);
std::cout << "size of before foo: " << foo.size() << '\n';
{
std::queue<int> bar;
swap(foo, bar);
}
std::cout << "size of after foo: " << foo.size() << '\n';
return 0;
}