which works beautifully for deque and vector and list:
1 2 3 4
vector<string> v;
slurp(v, cin);
Now obviously this won't work for, say, a map, which makes sense.
but when you erroneously pass a map you get a long compiler error trace.
NOW is there a way to catch this?
i.e. if there isn't a push_back member I can give a nice error message rather
than a scary long list of compiler errors?
(I am bound to forget what this template does in the future of course)
yes, that would work but it's not generic enough.
if there was a later container that had a correct interface (i.e. a push_back())
I would have to add it manually.
This will work on any STL containers. Note: std::map and std::multimap needs completely different care, as it is has key-value pairs, not just values. You can make the exact same code to work with std::map<K, V> and std::multimap<K, V> by overloading std::istream& operator>>(std::istream&, std::pair<K,V>&);
much better, but still doesn't solve the problem.
catch wrong containers neatly with a nice error message like:
"oops cannot use a map here" rather than the usual compiler errors.
I tried to use template meta programming and iterator traits but my head exploded!