Topic: Proposal for routing stdin directly to the formal params of a function


Author: "Anders Dalvander" <google@dalvander.com>
Date: Sat, 6 Aug 2005 17:42:17 CST
Raw View
This should do the trick:

template <typename T>
T read_from_cin()
{
   T t;
   if (std::cin >> t)
      return t;
   throw std::ios_base::failure("read failed");
}

foo(read_from_cin<int>());

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "msalters" <Michiel.Salters@logicacmg.com>
Date: Wed, 10 Aug 2005 00:43:15 CST
Raw View
Anders Dalvander schreef:

> This should do the trick:
>
> template <typename T>
> T read_from_cin()
> {
>    T t;
>    if (std::cin >> t)
>       return t;
>    throw std::ios_base::failure("read failed");
> }

For one parameter. For multiple parameters, you cannot
guarantee order of evaluation. However, streams do have
a definite order!

Furthermore, template argument deducation cannot be used,
so you have to specify the types of all function arguments.

Regards,
Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "vish" <vishakha@gmail.com>
Date: Thu, 4 Aug 2005 10:00:44 CST
Raw View
When the system invokes the main() of an app, and passes the cmd line
args, it appears as though the stdin is routed to the function main.
Continuing on the same lines of logic, it would be great to have the
stdin routed directly to a functions formal parameter, when definition
of an actual parameter is not required.
for eg. a syntax such as,
          foo(cin>>)  for a single param and multiple params separated
by a comma

the function declaration would decide which of the cin overloads to
use.
Any comments ?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "msalters" <Michiel.Salters@logicacmg.com>
Date: Thu, 4 Aug 2005 12:07:57 CST
Raw View
vish schreef:

> When the system invokes the main() of an app, and passes the cmd line
> args, it appears as though the stdin is routed to the function main.
> Continuing on the same lines of logic, it would be great to have the
> stdin routed directly to a functions formal parameter, when definition
> of an actual parameter is not required.
> for eg. a syntax such as,
>           foo(cin>>)  for a single param and multiple params separated
> by a comma
>
> the function declaration would decide which of the cin overloads to
> use.

Doesn't work as proposed. The (cin >> a >> b) syntax requires that
a and b are previously defined. That means they're initialized in
some way. If the extraction fails, they still have a value. In
function calls, the formal parameters are intialized with the
actual arguments.

E.g. the difference is similar to:
int a;
a = foo();

vs.
int a = foo();

OTOH, it's easy to write a trivial wrapper void foo( istream& ).
With Template Argument Deduction, you can even write a generic
function call_from_stream() which allows you to call
call_from_stream( cin, &foo ). It would use TAD to find out the
argument types T1...Tn of foo, define variables t1..tn with types
T1..Tn and call cin >> t1 ... >> tn; before returning foo(t1,...tn);
That's almost the same. If this proves really useful we could
introduce the syntactic sugar you proposed with this semantics.
Still, should one call foo() if extraction fails?

HTH,
Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "vish" <vishakha@gmail.com>
Date: Fri, 5 Aug 2005 12:57:33 CST
Raw View
Agreed that cin>>a>>b requires that a and b are previously defined. But
whenever we use pass by value a temporary object is created. for eg
X(int);
X(2) will create a temporary integer before passing it to the function,
same holds true for an expression.(X(<expression>))
Since C++ supports expressions, "cin>>" can also be cosidered as an
expression to be evaluated and result assigned to a temporary object
thats in turn passed to the function.

For instance,
foo(int);

foo(cin>>);
translates to
                    int temp;
                    if(cin >> temp)
                          foo(temp);
                   else
                         <throw>

As Michiel says, this can be indirectly done by writing a wrapper
call_from_stream(), but it would be nice to have a direct method.


-vi sh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]