Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ boost forward declaration question

I spend some time examining boost:: libraries architecture and was interested with the following fact:

In some parts of the libraries a yyy_fwd.hpp idea is used pretty common (see boost/detail or boost/flyweight for examples).

These files obviously contain only forward declarations of some template-based classes and as far as I understand, could benefit in terms of compilation time.

Could someone point out in what cases do they help and should I use the same idea when designing my own templates?

Thank you.

like image 843
Yippie-Ki-Yay Avatar asked Aug 05 '10 12:08

Yippie-Ki-Yay


People also ask

Does C support forward declaration?

Classes. In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.

Where do you put forward declaration?

Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.

Why do we do forward declaration in C++?

A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.

How do you forward a declaration function in C++?

To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.


1 Answers

Forward declarations are needed to reduce compile-time dependencies. For instance, when implementing Pimpl idiom.

One more case is that, for instance, boost::pool* depends on windows.h on Windows platform. When creating my interface I don't want to force the users of my class to include the system headers by using my interface.


*Ok, that's a bad example, because boost/poolfwd.hpp still includes windows.h, but I hope they'll fix this issue. And I hope you get the idea.

like image 81
Kirill V. Lyadvinsky Avatar answered Sep 24 '22 20:09

Kirill V. Lyadvinsky