26

I intended to call a private class member function, but by a copy&paste mistake pasted the line as this function is declared in the header file:

void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
    void updateAxisEnabled();
}

instead of

void DebugView::on_cbYAxisEnabled_stateChanged(int)
{
    updateAxisEnabled();
}

Surprisingly, the code was compiled and executed. However the method updateAxisEnabled() was not executed.

So, why does it compile? Was here a local function declared within a method body or has void instructed the compiler to ignore whatever comes afterwards?

The compiler is Visual Studio 2008.

P.S.: I'm aware of class declaration/definition within functions, but not functions within functions in C++.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • 4
    `P.S.: I'm aware of class declaration/definition within functions, but not functions within functions in C++` Well, now you know that's possible too. – deviantfan Oct 13 '15 at 12:38
  • @deviantfan: I'm sure, that it is not legal to DEFINE a local function in C++. So what is the point to DELCARE a local function without being able to define a body for it? – Valentin H Oct 13 '15 at 12:41
  • @ValentinHeinitz Have you tried with g++ for example,I am curious how that would work. – Richard Rublev Oct 13 '15 at 12:43
  • 2
    The same reason you forward declare anything; telling the compiler that a symbol's definition exists elsewhere. – AndyG Oct 13 '15 at 12:43
  • @RichardRublev: yes, seems to work, Tried here: https://gcc.godbolt.org/ – Valentin H Oct 13 '15 at 12:45
  • @AndyG: Ok, got it! The scope is global, so there is no magic here actually. – Valentin H Oct 13 '15 at 12:47
  • I think this a variant of the classic "vexing parse" issue. See e.g. [here](http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work) – user786653 Oct 13 '15 at 17:50
  • @user786653: I don't think, the issues are related. The issue you meant, is focused on "()" is not an expression in C++. Here I was not aware, that forward declaration is possible within a member function. It is clear, that whatever looks like function declaration in C++ will be taken by the compiler as function. A a(); in the case of the question you mentioned is obviously a function declaration, not the object creation of class A. – Valentin H Oct 13 '15 at 20:02
  • @ValentinHeinitz: Of course it is related, if local function declarations were not allowed, the most vexing parse would cease to be ambiguous. – Ben Voigt Oct 13 '15 at 21:38

2 Answers2

38

void updateAxisEnabled(); is a function declaration.

Sample:

#include <cstdio>

void a();
void b();

int main(void) {
    a();
    b();
    return 0;
}

void a() {
    void c(); // Declaration
    c(); // Call it
}

void b() {
    c(); // Error: not declared
}

void c() {
    puts("Hello, world!");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Cool! It works. I've tried it here: https://gcc.godbolt.org/. Good to know, that it's possible to make forward declaration in a member function. Thanks! – Valentin H Oct 13 '15 at 12:49
5

It is perfectly allowed to declare a function inside a function scope: a function may be declared in any scope.

A common mistake among C++ programmers is indeed to:

void foo()
{
    MyObject bar(); // 1
    bar.someMethod(); // 2
}

this will miserably fail to compile because line 1 is not declaring a MyObject named bar and calling its constructor explicitly; rather, it is declaring a function named bar that returns a MyObject. Thefore, there is really no object to call someMethod on.

edmz
  • 8,220
  • 2
  • 26
  • 45