Should one forward declare classes from a namespaced library?

332 views Asked by At

I'm designing a C++ library that will be placed inside a namespace.

If the users of my library only need a forward declaration of one of my classes and since you can't do forward declarations of things inside a namespace, e.g. class ns_name::class_name;, should I

  • tell them to include the whole header file that contains that class instead,
  • or, provide them with a way to forward declare stuff from my library? For example:

    #define MD_FORWARD_DECLARE(x) namespace md { x; }
    

    Which could then be used like this:

    MD_FORWARD_DECLARE(class foo)
    

    Is it worth it?

  • Or, just let them do namespace md { class foo; } themselves?
  • Or, as mentioned by DevSolar, make a dedicated header file consisting of forward declarations, like <iosfwd>? This seems most elegant to me.
1

There are 1 answers

1
kreuzerkrieg On BEST ANSWER

As @molbdnilo pointed out, there is nothing wrong with forward declaring with namespace. First option is not an option at all, for various reasons I dont want to include header until I have to, forward declaration is always preferred way. Why dont you just provide a header with forward declarations as many boost implementations do? for example boost spirit numerics_fwd.hpp?

Ah, missed @DevSolar comment. IMHO this is the best solution.