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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With