Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How to create a collection of stream objects




As most know it's not possible to have a standard collection of references. It's also not possible to copy a stream object.

But what if I want to make a collection (say a std::vector) of stream objects or stream object references?

I know I can wrap the stream object reference in e.g. a structure, but then you either need to implement the complete interface (if you want to use the wrapper directly as a stream, which I would prefer), or use a public getter function and use that everywhere to get the actual stream.

Is there a simpler way? C++11 solutions are okay.

like image 534
Some programmer dude Avatar asked Mar 25 '23 09:03

Some programmer dude

1 Answers

You can't have a container of references, but you can have a container of std::reference_wrapper. Perhaps you want something like:

std::vector<std::reference_wrapper<stream_type>> v;

You can treat a std::reference_wrapper very much like a reference (in fact, it is implicitly convertible to a reference type), but it has the extra advantage of being an object type.

like image 184
Joseph Mansfield Avatar answered Mar 31 '23 14:03

Joseph Mansfield