For some time it has been bothering me that there is apparently no way to directly initialize variables from input streams (something like int i << std::cin;)My solution is this:#include <iostream>#include <iomanip>#include <string>template<class T>T get(std::istream& in){    T tmp;    in >> tmp;    return tmp;}template<class T,class A, class... ARGS>T get(std::istream& in, A& arg, ARGS&... args){    in >> arg;    return get<T>(in, args...);}int main() {    int i = get<int>(std::cin);    std::cout << i << std::endl;    std::string s = get<std::string>(std::cin , std::setw(5));    std::cout << s << std::endl;}I'd like to know if I can improve something on the design There are serious performance implicationsThere are corner cases where this will not work or even worse produce silent errors.I've overlooked any standard functions/classes that already implement that functionality.
Directly initialize variable from std::istream
c++;io
There are corner cases where this will not work or even worse produce silent errors.The function as it is, depends on the client to check for errors on the stream (either by calling std::cin.exceptions(std::ifstream::failbit); or by checking the stream state, after the call to get).Ideally, I would want to write:if(auto a = get<int>(std::cin))    // use a hereMaybe you could add something like this to your solution:template<class T>boost::optional<T> get_optional(std::istream& in){    T tmp;    in >> tmp;    return (in) ?        boost::optional<T>{ std::move(tmp) } :        boost::optional<T>{};}