3

So in this another old huge C++ codebase I'm spotting this style quite often:

// ...

void FooBar::Eggs(int spam);
void FooBar::Eggs(int spam) {
    // implementation here
    // ...
}

In general, I do understand what's the point of forward declaring a function in C++, however I can't find any justification for this kind of duplication. Is there any reason to not eliminate it?

Community
  • 1
  • 1
ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • Well, it's a traditionally-old, with remnants of pre-standard C still there, so I'm generally trying to account for any possible legacy reasons. But still, this case seems way too strange style to me. – ulidtko May 27 '13 at 14:30
  • Is this not some kind of a pythonic scam? – hetepeperfan May 27 '13 at 14:31
  • @hetepeperfan, uhh... Pythonic what. – ulidtko May 27 '13 at 14:33
  • I would recommend enabling _-pedantic_ but i'm afraid it would cause the Universe to implode. – Captain Obvlious May 27 '13 at 14:34
  • @ulidtko I was refering to http://docs.python.org/2/tutorial/introduction.html#lists and that it is encouraged to make references to monty python in your code. See the list exmple there. – hetepeperfan May 27 '13 at 14:38

1 Answers1

5

If this is a codebase that predates standard C, it is possible that this code originally looked something like this:

#ifdef PROTOTYPES
void FooBar_Eggs(int spam);
#endif
FooBar_Eggs(spam)
     int spam;
{
    ...;
}

1990s-era C compilers, even if they supported prototypes, would not deduce a prototype from an "old-style" function definition for the benefit of code below it in the same file, so you would pretty regularly see this sort of construct in code intended to compile both with standard-compliant and legacy compilers.

Now imagine that at some point between then and now someone mechanically converted all the old-style function definitions to prototyped definitions, and someone else mechanically converted the C to C++, and the result might well look like what you have.

The leading declaration serves no purpose anymore and can safely be removed.

zwol
  • 135,547
  • 38
  • 252
  • 361