Function declaration inside a function definition

Back to programming

// Functions can be declared inside a
// function definition. The declaration
// is scoped normally.

class T
{
    int a_;
};

int main() 
{
    void f();
    // Compiles fine.
    f();

    // This is the classical trap:
    // it is not a construction, but
    // a function declaration.

    T t();

    // Does not compile, t is
    // not an object of type T.
    //t.a_ = 4;

    return 0; 
} 

void g()
{
    // Does not compile, because
    // there is no declaration of f
    // in scope.
    //f();
}

void f()
{
}