Hi all!

I've read forward declaration is much prefered than include in header files mainly because it reduces the compilation time. Other reasons?

My particular situation is that all header files that I use are within a namespace. I've seen two different ways for forward declaration in this case:

namespace n1
{
class c1;
}

and

using namespace n1;
class c1;

I think the first one is better because I don't like the idea of the "using" keyword in a .h file. But with this method 1 line of include is converted into 4 lines for each include

Since I use 5 or more includes of 5 different namespaces the code increases quite a lot.

My question is...
Is there any other way of using this forward declaration but with that increment in source code lines?

any idea?

thank you all in advance!

Dani AI

Generated

— good question and good thread. Forward declarations are primarily about decoupling headers so fewer translation units recompile when an implementation changes; that’s why projects and tools promote “include what you use” and replacing needless includes with forward-decls to speed builds and reduce header bloat. (include-what-you-use.org)

Basic rules to keep in mind: a forward-declared class is an incomplete type — it's fine as the type of a pointer/reference or for an argument/return by pointer/reference, but not for by-value members/parameters, sizeof, member access, inheritance, or when the definition must be known to instantiate templates. Template class declarations can be forward-declared, but some template uses still require the full definition. These limits are why forward-declaring helps but can't replace includes in all places. ()

About namespaces: avoid putting a global using namespace in a header (it pollutes every includer and is widely considered bad practice). As noted, whitespace can be compressed; as suggested, qualified declarations can be attempted, but forward-declaring with a qualified-id (the “class N::C;” trick) is fragile/implementation-dependent in various situations. A compact, modern alternative is the C++17 nested-namespace form to keep lines short while remaining portable, for example:

namespace company::module::ui { class Widget; }

This keeps the type in its namespace without a using-directive. (learncpp.com)

Practical options when forward-decls still feel verbose: group related forward-decls, adopt the Pimpl (opaque pointer) idiom for large classes that hide many private types, or use IWYU / precompiled headers to manage include cost. Pimpl moves private members into an implementation class (forward-declared in the header) and avoids exposing many headers in the public API; see the C++ Core Guidelines for the pattern and tradeoffs. (microsoft.github.io)

Summary checklist: prefer forward-declare for pointer/reference members and function signatures; include the real header when the full type is required; never use broad using namespace in .h; use nested-namespace shorthand (C++17+) or Pimpl when line-count or compile-time matters.

Recommended Answers

All 5 Replies

namespace n1 { class c1; } will not work for you?
Whitespace is a user interface thing; C++ cares little for it.

If you don't want to have the using directive in the header file you can just use class n1::c1; that's the preferred solution as far as I know.

namespace n1 { class c1; } will not work for you?
Whitespace is a user interface thing; C++ cares little for it.

Yes, I think I will do something like that. Though I don't like it much. I was just wondering is there was another approach

If you don't want to have the using directive in the header file you can just use class n1::c1; that's the preferred solution as far as I know.

That would be a clean solution but unfortunately does not compile with mi gcc

Thank you both!

Hi all!

I've read forward declaration is much prefered than include in header files mainly because it reduces the compilation time. Other reasons?

My particular situation is that all header files that I use are within a namespace. I've seen two different ways for forward declaration in this case:

namespace n1
{
class c1;
}

and

using namespace n1;
class c1;

I think the first one is better because I don't like the idea of the "using" keyword in a .h file. But with this method 1 line of include is converted into 4 lines for each include

Since I use 5 or more includes of 5 different namespaces the code increases quite a lot.

My question is...
Is there any other way of using this forward declaration but with that increment in source code lines?

any idea?

thank you all in advance!

Your first and second examples are NOT the same thing. In the second example class c1 is not declared to be in the namespace, while in the first example it is.


Lets assume you have this header file

// forward declaration
namespace n2
{
    class c2;
};

// this class uses the forward declaration
namespace n
{
    class c1
    {
    public:
    private:
        n2::c2* pC2; // use forward declaration
    };
};

Now in the *.cpp file just do this

#include "test.h"

int main()
{
    n::c1 object;
}

Your first and second examples are NOT the same thing. In the second example class c1 is not declared to be in the namespace, while in the first example it is.

You are absolutely right, it is good to know more about this topic which I ignored before

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.