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.
加载中,请稍侯......
精彩评论