I found this question and am completely puzzled.
The response says b is false: "Non-static members cannot be used as default arguments." It makes sense.
What I don't understand is why the other two are fine. In fact, I'm trying to understand what semantics is, if the default is not a constant expression ...
What's going on here? The default parameters are clearly evaluated at compile time. Does the compiler just 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 << fa() << std::endl; std::cout << fb() << std::endl; std::cout << fc() << std::endl; return 0; }
c ++ default-arguments
Karoly Horvath Nov 19 '14 at 13:37 2014-11-19 13:37
source share