473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to pass the function 'endl' as a parameter?

dear all,

i want to write log on disk file when macro NEED_DO_LOG is defined.
otherwise, do nothing.
so i wrote code like this:
#ifdef NEED_DO_LOG
fstream fsLogFile (...);
#else
CDummyStream fsLogFile;
#endif // #ifdef ASSAENGINE_NEED_DO_LOG

what i need to do now is making the CDummyStream acts like a fstream.
i wrote these code:
template <typename T>
CDummyStream& operator << (CDummyStream& dummy, const T& t)
{
return dummy;
}
above code can handle int, double, etc. but it can't handle the
function 'endl'.
if i write:
fsLogFile << endl;
the vs.net2003 compiler complains it could not deduce template argument
for 'overloaded function type' from 'overloaded function type'. what's
the meaning of that?

is that possible to pass endl as a parameter?

thanks.

Jun 7 '06 #1
8 2912
Considered overloading for char *, and passing in "\n" ?

Jun 7 '06 #2

Lally wrote:
Considered overloading for char *, and passing in "\n" ?


thanks for your reply.

but the endl function will call the flush funcion automatically to
ensure the data is written into disk file.

if i just pass "\n", the data may not be written into disk file
instantly, if the program crashs i may lost my log, that would be a big
trouble.

Jun 7 '06 #3
Leo jay wrote:
dear all,

i want to write log on disk file when macro NEED_DO_LOG is defined.
otherwise, do nothing.
so i wrote code like this:
#ifdef NEED_DO_LOG
fstream fsLogFile (...);
#else
CDummyStream fsLogFile;
#endif // #ifdef ASSAENGINE_NEED_DO_LOG

what i need to do now is making the CDummyStream acts like a fstream.
i wrote these code:
template <typename T>
CDummyStream& operator << (CDummyStream& dummy, const T& t)
{
return dummy;
}
above code can handle int, double, etc. but it can't handle the
function 'endl'.
if i write:
fsLogFile << endl;
the vs.net2003 compiler complains it could not deduce template argument
for 'overloaded function type' from 'overloaded function type'. what's
the meaning of that?

is that possible to pass endl as a parameter?
Overload the operator<< of course:

CDummyStream& operator<<(const CDummyStream& c,
std::ostream& (*fn)(std::ostream&))
{
return c;
}

thanks.

Jun 7 '06 #4
benben wrote:
Overload the operator<< of course:

CDummyStream& operator<<(const CDummyStream& c,
std::ostream& (*fn)(std::ostream&))
{
return c;
}

thanks.


yes!!, what's what i wanted!

the return type should be const too.

why i can't use template?
is there any significant difference between:
const CDummyStream& operator<<(const CDummyStream& c,
std::ostream& (*fn)(std::ostream&))
{
return c;
}
and
template<typename T>
const CDummyStream& operator<<(const CDummyStream& c,
T& (*f)(T&))
{
return c;
}
why the second one can't be compiled?

Jun 7 '06 #5
Leo jay wrote:
dear all,

i want to write log on disk file when macro NEED_DO_LOG is defined.
otherwise, do nothing.
so i wrote code like this:
#ifdef NEED_DO_LOG
fstream fsLogFile (...);
Shouldn't a log file be an ofstream rather than an fstream?
#else
CDummyStream fsLogFile;
#endif // #ifdef ASSAENGINE_NEED_DO_LOG

what i need to do now is making the CDummyStream acts like a fstream.
i wrote these code:
template <typename T>
CDummyStream& operator << (CDummyStream& dummy, const T& t)
{
return dummy;
}
above code can handle int, double, etc. but it can't handle the
function 'endl'.
if i write:
fsLogFile << endl;
the vs.net2003 compiler complains it could not deduce template argument
for 'overloaded function type' from 'overloaded function type'. what's
the meaning of that?

is that possible to pass endl as a parameter?


You need a few more overloads, to help the compiler with template
argument deduction (remembering that endl is a templated function, so
the compiler doesn't know which specialization to use when all that is
requested is a "T"):

inline CDummyStream& operator << (CDummyStream& dummy, std::ios_base&
(*manip)(std::ios_base&))
{
return dummy;
}
inline CDummyStream& operator << (CDummyStream& dummy,
std::basic_ios<char>& (*manip)(std::basic_ios<char>&))
{
return dummy;
}
inline CDummyStream& operator << (CDummyStream& dummy, std::ostream&
(*manip)(std::ostream&))
{
return dummy;
}

You should probably also make CDummyStream extend std::ostream, if it
doesn't already, and give it a NULL streambuf (e.g. construct the
std::ostream base class with std::ostream(NULL)), to be on the safe
side. Is threadsafety an issue for you?

Tom
Jun 7 '06 #6
Leo jay wrote:
benben wrote:
Overload the operator<< of course:

CDummyStream& operator<<(const CDummyStream& c,
std::ostream& (*fn)(std::ostream&))
{
return c;
}
thanks.


yes!!, what's what i wanted!

the return type should be const too.

why i can't use template?
is there any significant difference between:
const CDummyStream& operator<<(const CDummyStream& c,
std::ostream& (*fn)(std::ostream&))
{
return c;
}
and
template<typename T>
const CDummyStream& operator<<(const CDummyStream& c,
T& (*f)(T&))
{
return c;
}
why the second one can't be compiled?


std::endl itself is overloaded. With your template version of operator
<< the compiler can't tell which endl it should pass.

Regards,
Ben
Jun 7 '06 #7

"Leo jay" <Py***********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
[snip
is that possible to pass endl as a parameter?

[snip]

--- C++ code ---
#include <iostream>
using namespace std;

void foo (ostream& manip (ostream&))
{
cout << manip << 17 << endl;
}

int main()
{
foo (endl);
return 0;
}
----------------

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jun 9 '06 #8
Leo jay wrote:
dear all,

i want to write log on disk file when macro NEED_DO_LOG is defined.
otherwise, do nothing.


Easiest solution is to use a null_streambuf when #ifndef NEED_DO_LOG.

HTH,
Michiel Salters

Jun 9 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
6
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I...
5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
9
by: matrim | last post by:
How do I call a function outside of main or use a parameter to pass a reference? If my function is: void getData(int& diameter, int& price) { cout << "Enter in the diameter of the pizza...
5
by: Dariusz Bismor | last post by:
Hi, Please help me to understand compiler behavior concerning the following code: class X{    int k; public:    X(){ k=1; }    X( X & model ){       k = model.k;
3
by: michael | last post by:
Hi All, How do I pass a reference to a pointer and update the pointer in the function? I have: void goGetString(char *str){ string inString; cin >inString; str = new char;
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.