Re: [PATCH 1/4] Add history graph API
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: 2016-06-15 22:44:27
On Sun, 6 Apr 2008, Johannes Schindelin wrote:
AFAICT you do not even need them then. Using "struct strbuf *" without ever declaring struct strbuf before that is perfectly valid.
In traditional C, and inside structure declarations etc, yes.
In modern C, in other contexts, no.
Modern C considers a function declaration to be its own scope (it's the
scope of the function definition, which in a declaration is obviously
just the declaration). So if you use a "struct xyzzy *" in a function
declaration, it will be a *different* "struct xyzzy *" from one declared
later.
Try to compile something like this:
int fn(struct xyzzy *);
int fn(struct xyzzy *);
with a modern C compiler, and it will actually say something along the
lines of "conflicting types for ‘fn’", because while the two declarations
look identical, they actually have two different (private) declarations of
"struct xyzzy" going on.
But to make it even more interesting, you don't actually need a full
declaration of "struct xyzzy" to make the compiler happy, you only need an
implicit one ahead of time. You can do that with the incomplete
declaration, of course (like the --graph patch did), ie just a simple
struct xyzzy;
before those declarations is sufficient, but so is the implicit
declaration of just using the pointer to it in some non-private scope, ie
it's equally valid to do
struct foobar {
struct xyzzy *ptr;
};
and this will already be enough to declare "struct xyzzy" in scope for the
function declarations afterwards.
Is this illogical? Somewhat. Why is it a private scope in a function
declaration but not in a struct declaration? Why isn't the function scope
limited to the stuff *inside* the function? Somebody probably knows, but
for the rest of us the answer is just "that's how it is, deal with it".
Linus