"christopher diggins" <cdiggins@videotron.ca> wrote in message news:sAYDd.32984$Y61.1203911@wagner.videotron.net. ..[color=blue]
> I just wanted to share this technique for piping the cout from one function
> to the cin of another, using the pipe operator:
>
> #include <iostream>
> #include <sstream>
>
> using namespace std;
>
> typedef void(*procedure)();
>
> class filter {
> public:
> filter(procedure x) : proc(x) { }
> void operator()(istream& in, ostream& out) {
> streambuf* inbuf = cin.rdbuf();
> streambuf* outbuf = cout.rdbuf();
> cin.rdbuf(in.rdbuf());
> cout.rdbuf(out.rdbuf());
> proc();
> cin.rdbuf(inbuf);
> cout.rdbuf(outbuf);
> }
> private:
> procedure proc;
> };
>
> void operator|(filter f1, filter f2) {
> stringstream s;
> f1(cin, s);
> s.seekg(0);
> f2(s, cout);
> }
>
> void HelloWorld() {
> cout << "hello world" << endl;
> }
>
> void CountChars() {
> string s;[/color]
--------------------------------[color=blue]
> s = cin.getline();[/color]
// Compiler g++ 3.3.3
In function `void CountChars()':
error: no matching function for call to `std::basic_istream<char,
std::char_traits<char> >::getline()'
/usr/include/c++/3.3.3/bits/istream.tcc:594: error: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3.3/istream:401: error:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int) [with _CharT = char, _Traits =
std::char_traits<char>]
--------------------------------[color=blue]
> cout << static_cast<int>(s.size()) << endl;
> }
>
> int main() {
> filter(HelloWorld) | filter(CountChars);
> return 0;
> }
>
> Any comments or suggestions?
>
> I am working on a version which allows passing of arguments to functions,
> and which allows arbitrary chaining.
>[/color]
[snip]
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn