#include #include #include #include #include class Config { std::vector names_; public: const std::vector& names() const noexcept { return names_; } void replace(const std::vector& input) { std::vector candidate; candidate.reserve(input.size()); for (const auto& name : input) { if (name.empty()) throw std::invalid_argument("empty name"); candidate.push_back(name); } names_.swap(candidate); } }; int main() { Config config; config.replace({"alpha", "beta"}); const auto before = config.names(); bool rejected = false; try { config.replace({"changed", ""}); } catch (const std::invalid_argument&) { rejected = true; } assert(rejected && config.names() == before); config.replace({}); assert(config.names().empty()); }