Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these default arguments allowed?

I've found this question, and I'm completely baffled.

The answer says b is invalid, "Non-static members can not be used as default arguments.". That makes perfect sense.

What I don't understand is why the other two are okay. In fact, I'm struggling to understand what the semantics are if the default is not a constant expression...

What's going on here? Default parameters are clearly evaluated at compile time. Does the compiler simply pick the current value?

#include <iostream>

int g_x = 44; 

struct Foo 
{ 
  int m_x; 
  static int s_x; 

  Foo(int x) : m_x(x) {} 
  int a(int x = g_x) { return x + 1; } 
  int b(int x = m_x) { return x + 1; }
  int c(int x = s_x) { return x + 1; }
}; 

int Foo::s_x = 22; 

int main(int argc, char** argv) 
{ 
  Foo f(6); 

  std::cout << f.a() << std::endl; 
  std::cout << f.b() << std::endl; 
  std::cout << f.c() << std::endl; 

  return 0; 
}
like image 393
Karoly Horvath Avatar asked Nov 19 '14 13:11

Karoly Horvath


1 Answers

Actually, default arguments are evaluated when the function is called, which is why this is okay. From the draft C++ standard section 8.3.6 Default arguments which says (emphasis mine going forward):

Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default argument, even if they are not evaluated. Parameters of a function declared before a default argument are in scope and can hide namespace and class member names.

The following example from the same section gives us a rationale for why we can use static members but not non-static ones:

[ Example: the declaration of X::mem1() in the following example is ill-formed because no object is supplied for the non-static member X::a used as an initializer.

int b;
class X {
    int a;
    int mem1(int i = a); // error: non-static member a
                         // used as default argument
    int mem2(int i = b); // OK; use X::b
    static int b;
};

The declaration of X::mem2() is meaningful, however, since no object is needed to access the static member X::b. Classes, objects, and members are described in Clause 9. —end example ]

like image 83
Shafik Yaghmour Avatar answered Sep 20 '22 22:09

Shafik Yaghmour