|
From http://www.kuzbass.ru:8086/docs/isocpp/decl.html#dcl.fct
A default argument expression is implicitly converted (clause conv) to the parameter type. The default argument expression has the same semantic constraints as the initializer expression in a declaration of a variable of the parameter type, using the copy-initialization semantics (dcl.init). The names in the expression are bound, and the semantic constraints are checked, at the point where the default argument expression appears. Name lookup and checking of semantic constraints for default arguments in function templates and in member functions of class templates are performed as described in temp.inst. [Example: in the following code, g will be called with the value f(1):
- int a = 1;
- int f(int);
- int g(int x = f(a)); // default argument: f(::a)
- void h() {
- a = 2;
- {
- int a = 3;
- g(); // g(f(::a))
- }
- }
- --- end example]
复制代码
In my experiment with following code:
- #include<iostream>
- using namespace std;
- int a = 1;
- int f(int);
- int g(int x = f(a)); // default argument: f(::a)
- int f(int i)
- {
- return i;
- }
- void
- h()
- {
- a = 2;
- {
- int a = 3;
- g(); // g(f(::a))
- }
- }
- int
- g(int x)
- {
- cout << "x is " << x << endl;
- }
- int
- main ()
- {
- h();
- }
复制代码
The result is:
x is 2
this shows that the default argument is f(2) but not f(1), for 'a' has been modified in function 'int h()'. Is it right? |
|