A
Alf P. Steinbach
* Bart van Ingen Schenau:
On the contrary, I don't see any technical problem implementing a default
argument mechanism that references 'this'.
It would be the only place in C++ where a default argument expression can
reference another argument, and would require the 'this' argument to be
evaluated first at least when there is such a default argument expression, but
'this' is treated specially in many other ways.
However, one might rationalize the existing rule as a consistency thing, that a
default argument expression is not allowed to reference other arguments, and/or
one might rationalize the existing rule as allowing any order of actual argument
evaluation (which I don't agree is a good thing, but it's been there since K&R).
Cheers,
- Alf
The problem is that the default argument expression is 'evaluated' in
two different contexts. At the point where the expression occurs in
the sourcce code, it is 'evaluated'to determine which objects and
functions are referenced. At this point, all identifiers must be bound
to an actual object or function.
When the default argument is used in a function call, then it is
evaluated for a second time, not to determine the actual value of the
default argument.
For example:
int a = 0;
int f(double x) {return x*100;}
int g(int x=f(a)); /* binds default argument to f(double) and ::a */
int f(int x) {return x;}
void m()
{
int a = 1;
::a = 2;
g(); /* calls g(f(2)) -> g(200) */
}
As you can see, this mechanism precludes using information that is
only known at the call site (such as the object on which a member-
function is being invoked).
On the contrary, I don't see any technical problem implementing a default
argument mechanism that references 'this'.
It would be the only place in C++ where a default argument expression can
reference another argument, and would require the 'this' argument to be
evaluated first at least when there is such a default argument expression, but
'this' is treated specially in many other ways.
However, one might rationalize the existing rule as a consistency thing, that a
default argument expression is not allowed to reference other arguments, and/or
one might rationalize the existing rule as allowing any order of actual argument
evaluation (which I don't agree is a good thing, but it's been there since K&R).
Cheers,
- Alf