Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I create a std::stack of std::ifstreams?

Why does the following not work:

#include <iostream>
#include <fstream>
#include <stack>

std::stack<std::ifstream> s;

-PT

like image 930
Prasoon Tiwari Avatar asked Mar 02 '10 13:03

Prasoon Tiwari


2 Answers

std::stack (like all STL containers) requires that its contained type be "assignable". In STL-speak, that means that it must have a copy constructor and an operator=. std::ifstream has neither of these.

You can imagine why you would not want to be able to copy and assign I/O streams; the semantics of what should happen when there are two copies of the same stream are not obvious. Should a read from or write to one copy affect the position of the other copy? Should closing one stream close the other? etc.

If you want to have "a container of std::ifstreams", then what you really should make is "a container of std::ifstream*s". Non-const pointers are always assignable. The caveat is that in this case of course you have to make sure that you delete the pointers yourself before destructing the container, since the container will not do that for you.

like image 101
Tyler McHenry Avatar answered Nov 12 '22 09:11

Tyler McHenry


Because streams are non copyable you can tecxhnicaly put them into standard containers.

But we can get around that by storeing pointers to the stream. But you dont want to store pointers to streams (especially if they are dynamically allocated) in a standard container. So we look to boost for a solution.

Boost has the concept of pointer containers.
This allows you to dynamically allocate an object and store the pointer in the pointer container which then takes ownership of the object and gives you access to the dynamic object as if it were the object (rather than a pointer).

Because the pointer container takes ownership you don;t need to worry about deleting the object. The container will do that.

Because it gives access to the contained objects as objects rather than pointers it allows you to use the stream in standard algorithms in a more natural fashon (campared with a container of pointers).

like image 2
Martin York Avatar answered Nov 12 '22 11:11

Martin York