开发者

Function declaration in function definintion

开发者 https://www.devze.com 2023-02-05 18:15 出处:网络
Is it legal C++ to have a functi开发者_StackOverflow社区on declaration in function definition?It\'s legal both as a declaration in the immediate block scope, and as a function definition as a member f

Is it legal C++ to have a functi开发者_StackOverflow社区on declaration in function definition?


It's legal both as a declaration in the immediate block scope, and as a function definition as a member function of a local class.

void f() {
  // this declares the function defined below (enclosing namespace)
  void g();
  g();
}

void g() {
  struct {
    void help() { ... }
  } h;
  h.help();
}


Yes, it is. Though this question would've been easy to answer. You could've just tried it and seen. In fact, the fact you can do this is one of the sources of an interesting C++ error:

class A {
 public:
   operator int() const { return 0; }
};
void joe()
{
     // Initializing an int?
     int fred(A());
}


Yes, it is legal.

Referring to the C++ spec, a the body of a function definition (8.4) contains a compound-statement, which in turn (6.3) contains one or more statements, one of which can be a declaration-statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消