 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
vish Guest
|
Posted: Thu Aug 04, 2005 4:00 pm Post subject: Proposal for routing stdin directly to the formal params of |
|
|
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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Aug 04, 2005 4:12 pm Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
vish wrote:
| Quote: | 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.
|
What does that mean "routed to the function"? 'stdin' is a file stream
opened for you by the library. It has really nothing to do with the
command-line arguments.
| Quote: | Continuing on the same lines of logic,
|
"Logic"?
| Quote: | it would be great to have the
stdin routed directly to a functions formal parameter, when definition
of an actual parameter is not required.
|
I am guessing that I will understand this as soon as you explain what it
means to "route stdin to a function" or "to a function formal parameter".
| Quote: | 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 ?
|
At this point I have the only comment: Huh? Could you please elaborate?
What I am reading is that if you invoke your program as
yourprogram a,b,c
you'd be calling 'foo("a","b","c")', and if the invocation of the program
looks like this
yourprogram onetwothree
the call to 'foo' would be 'foo("onetwothree")'. Is that something you
have in mind? It has nothing to do with C++, though, because it would
require the program to be _interpreted_ rather than _compiled_ letting the
overload resolution be _deferred_ until run-time. That's not C++ and I
won't be going on a limb to state that it will never be, I am sure.
V
---
[ 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 ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Thu Aug 04, 2005 6:07 pm Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
vish schreef:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
vish Guest
|
Posted: Fri Aug 05, 2005 6:57 pm Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
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 ]
|
|
| Back to top |
|
 |
Anders Dalvander Guest
|
Posted: Sat Aug 06, 2005 11:42 pm Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
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 ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Sun Aug 07, 2005 1:27 am Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
vish wrote:
| Quote: | For instance,
foo(int);
foo(cin>>);
translates to
int temp;
if(cin >> temp)
foo(temp);
else
throw
|
What you can already do in today's C++ is
class read_from
{
std::istream& in;
public:
read_from(std::istream& in) : in(in) {}
template<typename T> operator T() const
{
T t;
if(in >> t)
return t;
else
throw exception_of_your_choice();
}
template<typename T> T as() const { return this->operator T(); }
}; // class read_from
..
void foo(int);
void bar(double);
unsigned long toto()
{
foo(read_from(std::cin)); // OK, reads an int
bar(read_from(std::cin)); // OK, reads a double
float f = read_from(std::cin); // OK, reads a float
return read_from(std::cin); // OK, reads an unsigned long
}
However, there is a problem with overloaded functions:
#include <cmath>
..
double x = std::sqrt(read_from(std::cin)); // Error: ambiguity between
// std::sqrt(float), std::sqrt(double) and std::sqrt(long double)
That's why I added the member function 'read_from::as()':
double x = std::sqrt(read_from(std::cin).as<double>()); // OK, reads a double
I don't know how you would solve the problem of overloaded (and perhaps templated)
functions with the new syntax you proposed...
Falk
---
[ 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 ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Wed Aug 10, 2005 6:43 am Post subject: Re: Proposal for routing stdin directly to the formal params |
|
|
Anders Dalvander schreef:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|