By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

legacy code?

P: n/a
static enum { IOERROR = -1, GOOD = 0, BAD = 1 } s_status = GOOD;

static ostream& err()
{
s_status = IOERROR;
return cerr << "Error: ";
}

static int missing(const char *argName, char option)
{
err() << "missing `" << argName << "' argument for -"
<< option << " option." << endl;
return s_status;
}

this is a code snippet from John Lakos'. Of course it is primitive and
doesn't compile with the latest compilers. It seems that function
evaluation is assumed from left to right.

Compilers don't use << on return value of err(), rather they try to use
operator << on err() itself. Is there a better way of doing this:

static int missing(const char *argName, char option)
{
ostream& temp(err());
temp << "missing `" << argName << "' argument for -"
<< option << " option." << endl;
return s_status;
}

Apr 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ro****@richfx.com wrote:
static enum { IOERROR = -1, GOOD = 0, BAD = 1 } s_status = GOOD;

static ostream& err()
{
s_status = IOERROR;
return cerr << "Error: ";
}

static int missing(const char *argName, char option)
{
err() << "missing `" << argName << "' argument for -"
<< option << " option." << endl;
return s_status;
}

this is a code snippet from John Lakos'. Of course it is primitive and
doesn't compile with the latest compilers. It seems that function
evaluation is assumed from left to right.

Compilers don't use << on return value of err(), rather they try to
use operator << on err() itself.
What does that mean, "on err() itself"? 'err()' is an expression. To
evaluate the the expression in first statement of the 'missing' function,
you need to evaluate every argument. 'err()' is just the left operand of
the left-most operator<<. The code created by the compiler will _have_to_
evaluate 'err()' before attempting to call the left-most operator<<. Now,
the rest of the operands in that whole expression can be evaluated before
'err()', but they don't depend on the result of evaluating 'err()', do
they?
Is there a better way of doing this:

static int missing(const char *argName, char option)
{
ostream& temp(err());
temp << "missing `" << argName << "' argument for -"
<< option << " option." << endl;
return s_status;
}


I don't see any difference. Yes, you force 'err()' to be evaluated before
the other expressions, but so what?

What are you trying to counter-act? It sounds like your optimizer somehow
screws up what's output when, and you get your "Error: " string somehow
injected between 'argName' or " option" or other output things. For all
I know about C++ that shouldn't happen. If that's what you're observing,
then your compiler's optimizer is broken.

Write a small _complete_ program that exhibits the problem you're trying
to solve (and do describe the problem better). Then we can talk a bit more.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 21 '06 #2

P: n/a

ro****@richfx.com wrote:
static enum { IOERROR = -1, GOOD = 0, BAD = 1 } s_status = GOOD;

static ostream& err()
{
s_status = IOERROR;
return cerr << "Error: ";
}

static int missing(const char *argName, char option)
{
err() << "missing `" << argName << "' argument for -"
<< option << " option." << endl;
return s_status;
}

this is a code snippet from John Lakos'. Of course it is primitive and
doesn't compile with the latest compilers. It seems that function
evaluation is assumed from left to right.


Actually this is legit C++ code and compiles just fine if you add
#include <iostream>
using namespace std; // or equivalent

Apr 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.