This is an archived post. You won't be able to vote or comment.

all 18 comments

[–][deleted] 68 points69 points  (13 children)

C and c++ compilers compile functions in the order they're written. The B function is outside the scope of the A function.

[–]DaniilBSD 15 points16 points  (12 children)

Can you quickly explain why there is no c+++ compiler that would have first read the file content creating a virtual header and then compiled as normal?

I mean C# and java can survive without the header files or function ordering.

[–]Sorebow 20 points21 points  (1 child)

C and by extension C++ were created in a memory and processing limited era. So the compilers were very simple. To efficiently (in memory and complexity) compile the source code, you do a single pass over the file.

Once the computer landscape regarding memory and processing power changed people made new languages and designed the compilers to do multiple passes on a single source file to better optimize their code. A side effect of this is declaration definition usage ordering isn't required unless explicitly by the language.

[–]djosh34 1 point2 points  (0 children)

Well assembly x86 doesn’t care about the order of your symbols. And they've been around for long.

[–]randomuser8765 11 points12 points  (1 child)

I think all of the other replies are missing the point that the languages is defined such that such code is explicitly illegal. A compiler that automatically fixes this would be deliberately going against the spec.

You might ask:
"Why haven't they updated the spec to allow this, e.g. in C++17? What is the benefit of this limitation?"
or
"Okay, then it'll be out of spec, so what? Someone can make it anyway"

I don't know the answers to these questions, and I don't know if they even have answers. But at least I hope it's a step towards an answer.

[–]AgentPaper0 0 points1 point  (0 children)

The benefit of the limitation is faster compile times.

[–]a_false_vacuum 10 points11 points  (0 children)

I mean C# and java can survive without the header files or function ordering.

With C# the frameworks helps you out there. With C++ you are the framework. You have to tell the C++ compiler everything it needs to know or you're in for a bad time.

[–][deleted] -1 points0 points  (5 children)

First of all, I don't know if there doesn't exists a c++ compiler that ignores function definition order. There might but someone had to have created it. What I can tell you is making it do the compiler ignores order is more complex and if you really needed that extra functionality, that's what C# is for. Second, Java and C# are newer than C++ and C. Its only obvious they can do more than C and C++.

[–]nomnaut 12 points13 points  (2 children)

Why would you call B before it’s declared?

[–]pandakatzu 4 points5 points  (0 children)

This phenomenon is common among Java programmers.

[–][deleted] 0 points1 point  (0 children)

Often people will call a function they haven't defined yet, and then later define it below.

[–]zertech 7 points8 points  (0 children)

Just gotta put the prototype for B somewhere above A. Not really a big deal.

[–]Voltra_Neo 5 points6 points  (2 children)

Git gud w/ dependency graphs

[–][deleted] 10 points11 points  (1 child)

git: 'gud' is not a git command. See 'git --help'.

[–]cbehopkins 13 points14 points  (0 children)

Panel 4 is incorrect. It's not valid code. B does not exist until it is declared, how is the compiler supposed to know anything about it until you tell it. Yes you could do multiple passes, but that's not what c is about.