Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ header-only include pattern

I would like to write code in .hpp without separation to .h and .cpp

  • I did it. I use .cpp only for static class-fields definitions

I would like not to write #include manually ...

  • I use forward delarations where it possible.
  • Every my .hpp file containt #pragma once.
  • But, when my project grows up to 40-50 classes i saw problem of include graph. There are some errors of definitions.

Image with include graph of my project model (like part of mvc) attached.
I used this app for graph generation (can work without MSVS!).

include graph

How include graph should look like? Like a tree?
How not to write includes manually, like in C# or Java?

like image 953
k06a Avatar asked Nov 24 '25 09:11

k06a


1 Answers

Unfortunately you're possibly using the wrong language. There are some things that are just much in C++ easier when you separate the class definition from implementation. Even with forward declarations you'll probably still wind up with circular dependencies that can only be resolved by moving implementations into separate files.

If you want to write idiomatic Java, just write it in Java. If you want to use the C++ language unfortunately you'll have to work within its constraints.

like image 106
Mark B Avatar answered Nov 26 '25 22:11

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!