{"type": "session", "harness": "opencode", "id": "cpp-concepts-realworld", "name": "Apply C++20 concepts for type-safe APIs and better error messages"} {"type": "message", "message": {"role": "system", "content": "You are an expert C++ developer. You write clean, efficient, and idiomatic code."}} {"type": "message", "message": {"role": "user", "content": "Apply C++20 concepts for type-safe APIs and better error messages"}} {"type": "message", "message": {"role": "assistant", "content": "#include \n#include \n#include \n#include \n#include \n\n// Concept definitions\ntemplate\nconcept Hashable = requires(T t) {\n { std::hash{}(t) } -> std::convertible_to;\n};\n\ntemplate\nconcept Container = requires(T t) {\n std::begin(t);\n std::end(t);\n t.size();\n};\n\ntemplate\nconcept Sortable = requires(T t) {\n std::sort(t.begin(), t.end());\n};\n\ntemplate\nconcept Numeric = std::integral || std::floating_point;\n\n// Constrained function template\nvoid printSorted(Sortable auto& container) {\n std::sort(container.begin(), container.end());\n for (const auto& item : container) std::cout << item << \" \";\n std::cout << \"\\n\";\n}\n\ntemplate\nT clamp(T value, T low, T high) {\n return std::max(low, std::min(value, high));\n}\n\n// Constrained class\ntemplate\nclass HashMap {\n std::vector> data;\npublic:\n void insert(K key, V value) {\n data.emplace_back(std::move(key), std::move(value));\n }\n V* find(const K& key) {\n for (auto& [k, v] : data) {\n if (k == key) return &v;\n }\n return nullptr;\n }\n};\n\nint main() {\n std::vector nums = {5, 3, 1, 4, 2};\n printSorted(nums);\n\n std::vector words = {\"cherry\", \"apple\", \"banana\"};\n printSorted(words);\n\n std::cout << clamp(15, 0, 10) << \"\\n\";\n\n HashMap map;\n map.insert(\"hello\", 42);\n if (auto* val = map.find(\"hello\")) {\n std::cout << \"Found: \" << *val << \"\\n\";\n }\n\n return 0;\n}"}}