diff --git a/bin/examples/concepts/main.cpp b/bin/examples/concepts/main.cpp index 894324b..ca5366d 100644 --- a/bin/examples/concepts/main.cpp +++ b/bin/examples/concepts/main.cpp @@ -4,29 +4,56 @@ template void example_has_value_type(const T& container) { + /* + typeid().name() - возвращает декорированное (mangled) имя типа, которое зависит от компилятора: + i = int (в GCC/G++) + d = double + f = float + c = char + Ss = std::string (в GCC) + NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE = тоже std::string (полное декорированное имя) + */ hack::log()("Value type: ", typeid(typename T::value_type).name()); } -// template -// void example_has_key_type(const T& container) { -// std::cout << "Key type: " << typeid(typename T::key_type).name() << std::endl; -// } -// -// template -// void example_has_mapped_type(const T& container) { -// std::cout << "Mapped type: " << typeid(typename T::mapped_type).name() << std::endl; -// } -// -// template -// void example_has_iterator(const T& container) { -// std::cout << "Container is iterable, size: " << std::distance(container.begin(), container.end()) << std::endl; -// } -// -// template -// void example_has_size(const T& container) { -// std::cout << "Container size: " << container.size() << std::endl; -// } -// +template +void example_has_key_type(const T& container) +{ + hack::log()("Key type: ", typeid(typename T::key_type).name()); +} + +template +void example_has_mapped_type(const T& container) +{ + hack::log()("Mapped type: ", typeid(typename T::mapped_type).name()); +} + +template +void example_has_iterator(const T& container) +{ + hack::log()("Container is iterable, size: ", std::distance(container.begin(), container.end())); +} + +template +void example_has_size(const T& container) +{ + hack::log()("Container size:", container.size()); +} + +template +void example_has_key_compare(const T& container) +{ + hack::log()("Key compare type: ", typeid(typename T::key_compare).name()); +} + +template +void example_has_allocator_type(const T& container) +{ + // Можно получить аллокатор из контейнера + typename T::allocator_type alloc = container.get_allocator(); + hack::log()("Allocator obtained successfully"); +} + // template // void example_is_string(const T& str) { // std::cout << "String content: " << str << " (length: " << str.length() << ")" << std::endl; @@ -154,24 +181,33 @@ auto main(int argc, char *argv[]) -> int // Базовые концепты std::vector vec = { 1, 2, 3 }; std::map map = {{ 1, "one" }, { 2, "two" }}; + std::set set = { 5, 3, 1, 4, 2 }; + std::unordered_map umap = {{ 1, "uno" }, { 2, "dos" }}; int a = 10; hack::log()("has_value_type:"); example_has_value_type(vec); // example_has_value_type(a); // выдаст ошибку т.к. у a нет встроенного value_type - // std::cout << "2. has_key_type: "; - // example_has_key_type(map); - // - // std::cout << "3. has_mapped_type: "; - // example_has_mapped_type(map); - // - // std::cout << "4. has_iterator: "; - // example_has_iterator(vec); - // - // std::cout << "5. has_size: "; - // example_has_size(vec); - // + hack::log()("has_key_type:"); + example_has_key_type(map); + + hack::log()("has_mapped_type:"); + example_has_mapped_type(map); + // example_has_mapped_type(set); // выдаст ошибку т.к. у set нет типа mapped_type. Этот тип характерен для контейнеров, которые хранят пары "ключ-значение". + + hack::log()("has_iterator:"); + example_has_iterator(vec); + + hack::log()("has_size:"); + example_has_size(vec); + + hack::log()("has_key_compare:"); + example_has_key_compare(map); + + hack::log()("has_allocator_type:"); + example_has_allocator_type(map); + // // Строки и контейнеры // std::string str = "Hello"; // std::list list = {1.1, 2.2, 3.3}; @@ -193,9 +229,6 @@ auto main(int argc, char *argv[]) -> int // std::stack stack; // stack.push(1); stack.push(2); // - // std::set set = {5, 3, 1, 4, 2}; - // std::unordered_map umap = {{1, "uno"}, {2, "dos"}}; - // // std::cout << "10. is_container_adapter: "; // example_is_container_adapter(stack); //