Prototype clones a Resource into a new unique_ptr with copied id/values. Final ResourceCache.acquire requires a nonempty ID, returns the existing shared allocation while alive or creates a Resource{id,{}} when no live owner exists. Cache entries are weak owners: dropping all caller owners must destroy the Resource. prune removes expired entries and returns the number removed, slots reports retained weak entries. Invalid acquire leaves state unchanged. Independent caches do not share resources.

C++ contract conventions: numeric values use signed Int (long long); all inputs and intermediate arithmetic are within its representable range. Opt<T> means std::optional<T> and absence is std::nullopt; Vec<T> means std::vector<T>. Invalid documented arguments throw std::invalid_argument. Inputs passed by const reference remain caller owned. Returned containers and records own their contents. No runtime type coercion is required; C++ static types exclude many dynamic-language type errors. Unless a lesson states otherwise, string ordering is lexicographic over Unicode code points (Text) or UTF-8 identifiers (std::string).

cpp
struct Resource {std::string id;Vec<Int> values;};
inline std::unique_ptr<Resource> prototype(const Resource& source);
class ResourceCache { public:
ResourceCache();
std::shared_ptr<Resource> acquire(const std::string& id);
Int prune();
Int slots() const;
};

Compile and run from this directory:

Shell
c++ -std=c++20 -O2 -Wall -Wextra -pedantic -pthread main.cpp -o practice
./practice --stage prototype
./practice --stage final

Edit exercise.hpp. Both stages start unfinished and must report failures until implemented. Keep main.cpp as the executable contract. The runner returns nonzero for a failed check, an unexpected exception, an invalid stage, or an empty suite. C++20 and only the standard library are required.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.

C++ runtime powered by Wasmer.