Why is the Google style guide discouraging forward declarations?

This is not to say that the Google style guide is a holy bible, but as a novice programmer, it seems like a good reference.

The Google Style Guide lists the following disadvantages of a forward declaration.

  • Advanced declarations can obscure the dependency, allowing the user code to skip the necessary recompilation when changing headers.

  • Subsequent changes to the library may be disrupted. Advanced declarations of functions and templates may prevent header owners from making other compatible changes to their APIs, such as expanding a parameter type, adding a template parameter with a default value, or moving it to a new namespace.

  • Forward the declaration of characters from the std :: yields undefined namespace.

  • It is difficult to determine if a forward declaration or full #include is required. Replacing #include with a declaration in the direct order can change the code value:

the code:

// bh: struct B {}; struct D : B {}; // good_user.cc: #include "bh" void f(B*); void f(void*); void test(D* x) { f(x); } // calls f(B*) 

If #include has been replaced with forward decls for B and D, test () will call f (void *).

  1. Forwarding the declaration of several characters from the header can be more verbose than simply #include the header.

  2. Structuring the code to include forward declarations (for example, using pointer elements instead of object members) can make the code slower and more complex.

However, some searches on SO seemed to suggest that forwarding declaration was a universal solution.

So, given these seemingly nontrivial flaws, can someone explain this discrepancy?

And when is it safe to ignore some or all of these shortcomings?

+8
c ++ google-style-guide forward-declaration
source share
2 answers

some search on SO seemed to suggest that forward declaration is universally the best solution.

I don't think what SO says. The text you are quoting compares the guerrilla ad with the inclusion of the corresponding include file. I don’t think you will find much SO support for the approach that Google criticizes here. This bad approach: "no, do not #include include files, just write declarations for several functions and types that you want to use."

The correct include file will still contain its own declarations, and a search on SO will tell you that it is correct, so I can see where you got the idea that SO stands for declarations. But Google does not say that the library’s own include file should not contain forward declarations stating that you should not go rogue and write your own declaration for each function or type that you want to use.

If you #include correct include file and your build chain is working, then the dependency is not hidden, and the remaining problems are mostly not applied, despite the fact that the include file contains declarations. There are still some difficulties, but that’s not what Google is talking about here.

In particular, if we are talking about forward type declarations compared to class definitions for them, (4) gives an example of what is going wrong (since a direct declaration of D cannot express that it was obtained from B , for this you need a class definition ) There is also a method called "Pimpl" that carefully uses direct type declarations for a specific purpose. So you will see some SO support for this, but it’s not the same as supporting the idea that everyone should generally run forward-declaring classes instead of #include with their header files.

+5
source share

From Titus Winters Discussion on CppCon 2014 :

The big one we learned recently: Forward announcing anything with a template in it is a really bad idea. This has led to problems with the service, as you will not believe. In some cases, a forward declaration may be okay? My suspicion is that the rule will really change to: Library owners are invited to provide a headline that specifically conveys ads that they think are worth (highlighted by me), and you probably shouldn’t make yourself known, and no one will ever must indicate a template type. We'll see. We are still developing [inaudible] details from what we have learned.

So maybe problems with trying to directly jump to declare template types may be one of their motives for discouraging wholesale advertising ...?

Also, providing a “headline that specifically conveys ads that they think is worth it” is similar to the <iosfwd> method, as described here (solution 2.2), here and here .


EDIT:

To be clear, I did not say that I agree or disagree with Google’s disappointment regarding the forward declaration. I was just trying to understand their rationale and made a comment about <iosfwd> .

Personally, I use forward declarations whenever I can, following the directions given later in the same GOTW above (solution 3)

Guideline: Never #include heading when a forward declaration is available.

but Winters' reasoning also seems to have some merit. I was working on code in which I forwarded declared template types from a third-party library, and the syntax became messy (I had not yet encountered the maintenance problems mentioned in Winters). OTOH, I'm not so sure that I will disappoint all the advanced declarations, as stated in the Google C ++ Style Guide, but I think what works for Google?

Disclaimer: I'm not an expert, still a student.

+3
source share

All Articles