简体   繁体   中英

Need validation on a claim from Go Lang

I have been lately looking into GoLang -- coming from C++ background-- I am reading a paper which allegedly explains the reasoning behind making Golang, here is its link: https://talks.golang.org/2012/splash.article

One of the claims being is, handling Dependencies (Package) in C and C++ is pain and takes on a #ifndef guard instance to state

The intent is that the C preprocessor reads in the file but disregards the contents on the second and subsequent readings of the file...

I referred a GCC page for the same, https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html .

so that if the header file appears in a subsequent #include directive and FOO is defined, then it is ignored and it doesn't preprocess or even re-open the file a second time

Go: "Reads in and disregard" vs GCC: it doesn't preprocess or even re-open the file a second time.

Doesn't contradict?

your thoughts are appreciated. Thanks for Reading my question.

The first passage is talking about a generic compiler, which, conceptually speaking, should read the contents of the file and disregard the contents (because they are #ifdef d out). That is, roughly, what the C standard specifies a compiler should do.

But practically everything in the C standard is under the "as if" rule - a compiler does not actually have to be implemented in the way suggested in the standard, so long as the end result it produces is exactly the same in every case. As such, GCC's particular implementation adds an optimization where, in cases where it can tell with certainty that the contents of the file would be disregarded, it doesn't actually read it. This is perfectly fine because it still behaves as if it has read the file but disregarded it.

Note that other compilers do not necessarily do the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM