Why can't member variables be used as default parameters for parameters?

Possible duplicate:
Non-stationary member as default argument for non-static member function

Correct me if I am wrong, but as I think the default options work as follows:

When the compiler sees a function call, it starts pushing parameters onto the stack. When it runs out of parameters, it will start pushing the default values ​​onto the stack until all the necessary parameters are populated (I know that this is a simplification, since the parameters are actually shifted from right to left, so it will start from the default values, but the idea is the same).

If so, why can't member variables be used as default values? It seems to me that since the compiler pushes them, as usual, to the call site, it should be able to resolve them just fine!

EDIT . Since the answers to my questions were misunderstood, let me clarify. I know that this is so, and I know what language is and is not allowed. My question is why the language designers decided not to allow this, as it seems to work fine.

+6
source share
4 answers

The essence of what you are asking can be redone into this simple example.

void foo(int a, int b = a); 

This is not allowed in C ++. C ++ does not allow default arguments to depend on other parameters.

Using class members as default arguments is only a special case of the above, since classes are accessed using the this pointer, and the this pointer is another hidden parameter of each non-static member function.

So the question is why

 void foo(int a, int b = a); 

is not allowed.

One obvious potential reason for rejecting this is that it will impose additional requirements on the order in which arguments are evaluated. As you know, in C ++ the order of evaluating the function argument is not specified - the compiler can evaluate the arguments in any order. However, in order to support the aforementioned default argument functionality, the compiler had to make sure that a evaluates to b . This seems like an excessive requirement that limits the typical freedom of evaluation order that we are used to seeing in C ++.

Please note that this

 int a; void foo(int b = a); 

allowed in C ++. And, obviously, it does not demonstrate the above assessment procedure.

+4
source

This post lists all the ways to set a default parameter - Must the required function parameters default by default in C ++?

It is not difficult to get around your needs.

 class A { int a; public: void f(int i); void f() { f(a); } }; 

gives you what you want.

+1
source

I believe that these are the most suitable paragraphs from the standard, especially Β§9:

Arguments

8.3.6 Default [dcl.fct.default]

Β§7 Local variables should not be used in the default argument

Β§9 [...] Similarly, a non-static member should not be used in the default argument, even if it is not evaluated, if it is not displayed as an id expression of an access expression to a class member (5.2. 5), or if it is not used to form pointer to an element (5.3.1).

+1
source

To summarize Nawaz's remark on a related question: calling void Foo::Bar(int a = this->member) really means void Foo__Bar(Foo* this, int a = this->member) . Obviously, the second argument cannot be evaluated before the first, which violates the C ++ axiom that compilers can evaluate arguments in any order.

+1
source

All Articles