You can serialize a `std::stack` by accessing the protected container
member.
One way to access it is by using a member data pointer.
#include <iostream>
#include <stack>
template< class Item, class Container >
auto container_of( std::stack<Item, Container> const& st )
-> Container const&
{
struct Access: std::stack<Item, Container>
{
using std::stack<Item, Container>::c;
};
return st.*&Access::c;
}
auto main()
-> int
{
std::stack<int> st;
for( const int v : { 3, 1, 4, 1, 5, 9 } ) { st.push( v ); }
for( int const v : container_of( st ) )
{
std::cout << v << "\n";
}
}
> As a morally straight boy scout, I'd like to
> be prepared for the case where someone wants to marshal a stack
> between programs.
>
> I didn't check, but possibly the same problem exists for std::queue.
Same solution.
Cheers & hth.,
- Alf