Question Details

No question body available.

Tags

c++ initialization c++20 undefined-behavior base-class

Answers (1)

Accepted Answer Available
Accepted Answer
July 14, 2025 Score: 3 Rep: 280,700 Quality: Expert Completeness: 60%

project.get() is unsafe to me (and can just be project).

To fix your problem, I'd do this:

struct HasProject {
  std::uniqueptr project;

[[nodiscard]] Project& makeProject(std::stringview path) { assert(!project); project= std::makeunique(path); return *project; }; };

class FakeResourceManager : private HasProject, public ResourceManager { public: FakeResourceManager() : ResourceManager(makeProject("/some/path")) {}

virtual ~FakeResourceManager(); // defaulted in cpp

void markResourceAsDirty(); // 'extension' of base functionality for testing };

we move the storage into an earlier than ResourceManager base class, and that base class is fully initialized by the time we construct ResourceManager.

We can handle failures of project construction within makeProject (throwing or whatever), and don't have a method that takes a uniqueptr but fails catastrophically on an nullptr.