Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Where to put? Where to put?

45 views
Skip to first unread message

Daniel

unread,
May 1, 2018, 12:19:21 PM5/1/18
to
Consider the header file A.hpp, which contains a definition of A,

class A
{
};

Now suppose we introduce a new header file B.hpp, which contains a definition of B that uses A,

#include <A.hpp>

struct B
{
void f()
{
A a;
}
};

Now, suppose it's desirable for our users that including A.hpp also brings
in B, and we particularly want to keep the name A for that header file.

Solution 1. include B.hpp at the bottom of A.hpp (but I've never actually seen anyone do this, include header files at the bottom.) Is this bad?

Solution 2 (seems to be the boost approach)

Move the definition of class A to a separate file, boost likes detail/A.hpp,
and rewrite A.hpp as

#include <detail/A.hpp>
#include <B.hpp>

Comments?

Daniel







Jorgen Grahn

unread,
May 2, 2018, 9:26:07 AM5/2/18
to
On Tue, 2018-05-01, Daniel wrote:
> Consider the header file A.hpp, which contains a definition of A,
>
> class A
> {
> };
>
> Now suppose we introduce a new header file B.hpp, which contains a
> definition of B that uses A,
>
> #include <A.hpp>
>
> struct B
> {
> void f()
> {
> A a;
> }
> };
>
> Now, suppose it's desirable for our users that including A.hpp also brings
> in B, and we particularly want to keep the name A for that header file.

Sounds a bit speculative. Any real-life examples?

You have people using A, and you introduce B which depends on A.
Seems to me if people are explicitly interested in B, they might as
well #include B.h.

I also believe it's not an error to define both A and B in A.h,
if B is some kind of utility closely related to A.

> Solution 1. include B.hpp at the bottom of A.hpp (but I've never
> actually seen anyone do this, include header files at the bottom.)
> Is this bad?
>
> Solution 2 (seems to be the boost approach)
>
> Move the definition of class A to a separate file, boost likes detail/A.hpp,
> and rewrite A.hpp as
>
> #include <detail/A.hpp>
> #include <B.hpp>
>
> Comments?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Daniel

unread,
May 2, 2018, 11:33:05 AM5/2/18
to
On Wednesday, May 2, 2018 at 9:26:07 AM UTC-4, Jorgen Grahn wrote:
> On Tue, 2018-05-01, Daniel wrote:
> > Consider the header file A.hpp, which contains a definition of A,
> >
> > class A
> > {
> > };
> >
> > Now suppose we introduce a new header file B.hpp, which contains a
> > definition of B that uses A,
> >
> > #include <A.hpp>
> >
> > struct B
> > {
> > void f()
> > {
> > A a;
> > }
> > };
> >
> > Now, suppose it's desirable for our users that including A.hpp also brings
> > in B, and we particularly want to keep the name A for that header file.
>
> Sounds a bit speculative. Any real-life examples?
>
Sure.

"A.hpp" is https://github.com/danielaparker/jsoncons/blob/master/include/jsoncons/json.hpp

B.hpp is https://github.com/danielaparker/jsoncons/blob/master/include/jsoncons/json_convert_traits.hpp

Examples:

https://github.com/danielaparker/jsoncons/blob/master/doc/ref/encode_json.md

I'm currently including B.hpp (json_convert_traits.hpp) at the bottom of A.hpp (json.hpp.)

Thanks for commenting.

Daniel

Jorgen Grahn

unread,
May 3, 2018, 7:08:03 AM5/3/18
to
Thanks! I had a quick look, but it was beyond me. I think someone
who gets the /intention/ of your code might have useful suggestions.
0 new messages