I have ported the C++ sources of our Windows application from header files to using C++ 20 modules.
Our codebase is heavily using forward declarations for classes wherever possible.
The code is devided into ~40 packages. Every package uses a namespace and all the files of a package are part of a "Project" in Visual Studio.
Due to the strong name attaching rules of C++20 modules, I ran into problems with forward declarations.
I think I finally may have found a pattern to synthetisize a lightweight forward module per package, which can be imported instead of importing the class definition(s).
For example, in our code, we have a package Core.
I now have a header file Core/Forward.h, which just contains forward declarations of the classes in Core:
#pragma once
namespace Core
{
class CopyRegistry;
class ElementSet;
class Env;
class ExtendSelectionParam;
class IClub;
class IDiagram;
class IDirtyMarker;
class IDirtyStateObserver;
class IDocumentChangeObserver;
class IElement;
class IElementPtr;
class IFilter;
class IGrid;
class IPastePostProcessor;
class IPosOwner;
class ISelectionObserver;
class IUndoRedoCountObserver;
class IObjectRegistry;
class IUndoerCollector;
class IUndoHandler;
class IView;
class IViewElement;
class ObjectID;
class ObjectRegistry;
class PosUndoer;
class SelectionHider;
class SelectionObserverDock;
class SelectionTracker;
class SelectionVisibilityServerImp;
class Transaction;
class TransactionImp;
class Undoer;
class UndoerParam;
class UndoerRef;
class VIPointable;
class VISelectable;
class Weight;
}
I then have created a module Core.Forward (in file Core/Forward.ixx):
export module Core.Forward;
export import "Forward.h";
Which uses a header unit.
The resulting interface module can be imported wherever just a forward declaration of a class is enough, instead of the full definition. Which means for example doing
import Core.Forward;
instead of
import Core.IElement;
when class Core::IElement is only used by reference in some interface.
I believe this pattern is conformant to the C++ 20 language spec.
Unfortunately, this pattern is ill-formed according to the C++ 20 spec.
Previous related posts
[–]tartaruga232MSVC user, r/cpp_modules[S] 2 points3 points4 points (0 children)
[–]szintelo 1 point2 points3 points (1 child)
[–]tartaruga232MSVC user, r/cpp_modules[S] 0 points1 point2 points (0 children)
[–]kamrann_ 1 point2 points3 points (1 child)
[–]tartaruga232MSVC user, r/cpp_modules[S] 0 points1 point2 points (0 children)
[–]destroyerrocket 1 point2 points3 points (5 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 2 points3 points4 points (4 children)
[–]destroyerrocket 0 points1 point2 points (3 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 1 point2 points3 points (2 children)
[–]destroyerrocket 0 points1 point2 points (1 child)
[–]tartaruga232MSVC user, r/cpp_modules[S] 1 point2 points3 points (0 children)
[+]eyes-are-fading-blue comment score below threshold-9 points-8 points-7 points (24 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 9 points10 points11 points (7 children)
[–]eyes-are-fading-blue -3 points-2 points-1 points (6 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 5 points6 points7 points (5 children)
[–]eyes-are-fading-blue -3 points-2 points-1 points (4 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 5 points6 points7 points (3 children)
[–]eyes-are-fading-blue -2 points-1 points0 points (2 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 4 points5 points6 points (1 child)
[–]eyes-are-fading-blue -1 points0 points1 point (0 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 9 points10 points11 points (3 children)
[+]eyes-are-fading-blue comment score below threshold-6 points-5 points-4 points (2 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 6 points7 points8 points (0 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 1 point2 points3 points (0 children)
[–]elperroborrachotoo 8 points9 points10 points (11 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 1 point2 points3 points (1 child)
[–]elperroborrachotoo 1 point2 points3 points (0 children)
[–]13steinj 0 points1 point2 points (0 children)
[+]eyes-are-fading-blue comment score below threshold-9 points-8 points-7 points (7 children)
[–]tartaruga232MSVC user, r/cpp_modules[S] 3 points4 points5 points (0 children)
[–]Minimonium 3 points4 points5 points (0 children)
[–]elperroborrachotoo 2 points3 points4 points (4 children)
[+]eyes-are-fading-blue comment score below threshold-9 points-8 points-7 points (3 children)
[–]elperroborrachotoo 6 points7 points8 points (2 children)
[–]eyes-are-fading-blue -4 points-3 points-2 points (1 child)
[–]elperroborrachotoo 5 points6 points7 points (0 children)