Topic: Proposal for routing stdin directly to the formal params of a
Author: tannhauser86549spam@free.fr (=?ISO-8859-1?Q?Falk_Tannh=E4user?=)
Date: Sun, 7 Aug 2005 01:27:16 GMT Raw View
vish wrote:
> 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 ]
Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Thu, 4 Aug 2005 16:12:24 GMT Raw View
vish wrote:
> 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.
> Continuing on the same lines of logic,
"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.
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".
> 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 ]