Nonstatic member as a default argument of a nonstatic member function

后端 未结 9 1061
攒了一身酷
攒了一身酷 2020-11-28 05:56
struct X
{
   X():mem(42){}
   void f(int param = mem) //ERROR
   {
      //do something
   }
private: 
   int mem;
};

Can anyone give me just one

9条回答
  •  渐次进展
    2020-11-28 06:21

    As DeadMG has mentioned above, somethig like

    void func(int i, int f = g(i))

    is illegal for the same reason. i suppose, however, that it is not simply a silly restriction. To allow such a construction, we need to restrict evaluation order for function parameters (as we need to calculate this before this->mem), but the c++ standard explicitly declines any assumptions on the evaluation order.

提交回复
热议问题