Accepting manipulators

M

Martin Eisenberg

What do I need to make this work?

#include <iostream>
#include <fstream>

using namespace std;

struct LoggedCout {
ofstream file;
LoggedCout(const char* name) : file(name, ios::app) {}
template<class T>
LoggedCout& operator<<(const T& t)
{ cout << t; file << t; return *this; }
};

LoggedCout lout("flushtest.txt");

int main() {
lout << "output";
lout << flush;
// no match for 'operator<<' in 'lout << std::flush'
{ char c; cin >> c; }
return 0;
}


Martin
 
V

Victor Bazarov

Martin said:
What do I need to make this work?

#include <iostream>
#include <fstream>

using namespace std;

struct LoggedCout {
ofstream file;
LoggedCout(const char* name) : file(name, ios::app) {}
template<class T>
LoggedCout& operator<<(const T& t)
{ cout << t; file << t; return *this; }
};

LoggedCout lout("flushtest.txt");

int main() {
lout << "output";
lout << flush;
// no match for 'operator<<' in 'lout << std::flush'
{ char c; cin >> c; }
return 0;
}

Was it you who asked a similar question about 'std::endl' yesterday?
If it was, why do you think it's different with 'flush'? It's just
another function pointer when used liek that in an expression.
If it wasn't, look in the archives for yesterday's postings here.
Please don't use this newsgroup as a write-only medium. Read it
before posting. Use Google Groups to read it beyond what your ISP's
news server retains.

V
 
M

mlimber

Martin said:
What do I need to make this work?

#include <iostream>
#include <fstream>

using namespace std;

struct LoggedCout {
ofstream file;
LoggedCout(const char* name) : file(name, ios::app) {}
template<class T>
LoggedCout& operator<<(const T& t)
{ cout << t; file << t; return *this; }
};

Add this to your class:

typedef ostream&(*IOManip)(ostream&);
LoggedCout& operator<<( const IOManip iomanip )
{
cout << iomanip;
file << iomanip;
return *this;
}

Or better, consider making it a non-member function since the whole
thing is public anyway.
LoggedCout lout("flushtest.txt");

int main() {
lout << "output";
lout << flush;
// no match for 'operator<<' in 'lout << std::flush'
{ char c; cin >> c; }
return 0;
} [...]

Sic, sed nemo id intelleget.

Cheers! --M
 
M

Martin Eisenberg

mlimber said:
Martin Eisenberg wrote:

Add this to your class:

typedef ostream&(*IOManip)(ostream&);
LoggedCout& operator<<( const IOManip iomanip )
{
cout << iomanip;
file << iomanip;
return *this;
}

Thanks! You're right, Victor, I should have seen that endl thread.
Let me change the question: why is T in my code not deduced as
something like IOManip above? Shouldn't it work as per 14.8.2/3?


Martin
 
V

Victor Bazarov

Martin said:
[..]
Let me change the question: why is T in my code not deduced as
something like IOManip above? Shouldn't it work as per 14.8.2/3?

You supply a pointer to function. What would you expect T to be?

Have you tried changing the argument to 'T' instead of 'const T&'?

Notice that 'IOManip' is not passed by reference.

V
 
M

Martin Eisenberg

Victor said:
Martin Eisenberg wrote:

You supply a pointer to function. What would you expect T to be?

Actually, I just realized that "flush" does not name a single entity
so there's no way to deduce anything at all in the context in
question. Right?


Martin
 
V

Victor Bazarov

Martin said:
Actually, I just realized that "flush" does not name a single entity
so there's no way to deduce anything at all in the context in
question. Right?

Probably.

'std::flush' is a template. 'flush' also is an 'std::basic_ostream'
member function. 'std::basic_ostream' declares at least three overloaded
operator << functions that take function pointers. As soon as you write
'flush' there, the compiler tries to understand which 'flush' you mean.
Most likely it finds the 'std::flush' template and tries to see if it
can figure out that template's arguments. In fact, it has to try the
same template arguments as the 'basic_ostream' has. It succeeds, AFAICT.

I am not sure my explanation makes sense. Josuttis probably has a better
one in his Standard Library book.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,081
Latest member
AnyaMerry

Latest Threads

Top