Re: Arguments in nested function calls
"Paul" <vhr@newsgroups.nospam> wrote in message
news:OnyRis3$GHA.1464@TK2MSFTNGP02.phx.gbl...
This is the implementation of class Field:
------------------------------------------------------------------
class Field {
friend std::ostream& operator <<(std::ostream&, const Field&);
public:
enum Format {F1, F2};
//...
Field& operator ()(Format f) { format = f; return *this; }
You should not set the format immediately here, but instead return a "stream
manipulator" that knows how to set the format. See setw, setprecision, etc
in <iomanip> for example. The help topic "Custom Manipulators with
Arguments" can be found at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfCustomManipulatorsWithArguments.asp.
//...
private:
//...
Format format;
//...
};
std::ostream& operator <<(std::ostream& out, const Field& f)
{
switch (f.format) {
case Field::F1:
return out << [f in F1 format];
case Field::F2:
return out << [f in F2 format];
default:
return out << [f in standard format];
}
}
------------------------------------------------------------------
and this is an example of its intended use:
------------------------------------------------------------------
Field f;
std::cout << "Field in F1 format: " << f(Field::F1) << f << ", Field in F2
format: " << f(Field::F2) << f << std::endl;
------------------------------------------------------------------
The problem is that if written as above, class Field will not be output in
two different formats but two times in the same (first) format. If the
expression is split into two, so that the overloaded operator ()(Format)
is called only once per expression, then the output will be as intended.
I am trying to understand what is happening. As I see it, the above is
equivalent (roughly) to this:
operator <<(operator <<(std::cout, f(Field::F1)), f)
(or, in other words, to a set of nested function calls)
If it were only one function - something along these lines
Field f;
void function1(Field& f1, const Field& f2, Field& f3, const Field& f4);
function1(f(Field::F1), f, f(Field::F2), f);
then, since the order of argument creation is unspecified but they will
have to be created before the function call is made, I would really expect
one of the formats passed to get the upper hand, so that eventual output
will only display Field in one format. In the original example, however,
we have a number of nested functions called in the prescribed order, so
that even though within a single function call the creation of arguments
is unspecified, the order of function calls is set and so, I would
imagine, the creation of sets of arguments passed to each.
Also, from what I have observed, if an argument is a temporary, its
destructor is invoked when the function returns
void f1(A1);
A1 f2(A2);
A2 a2;
f1(f2(a2));
(so a2's destructor is invoked when f2 returns rather than f1)
If this is correct, a temporary's "scope" in a function call is until that
function returns, which again does not take me to some sort of a
conclusion as to why in the original example the argument appears to be
shared across function calls.
Paul