end concepts examples
This commit is contained in:
@@ -54,244 +54,179 @@ void example_has_allocator_type(const T& container)
|
||||
hack::log()("Allocator obtained successfully");
|
||||
}
|
||||
|
||||
// template<hack::concepts::modern::is_string T>
|
||||
// void example_is_string(const T& str) {
|
||||
// std::cout << "String content: " << str << " (length: " << str.length() << ")" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_sequence_container T>
|
||||
// void example_is_sequence_container(const T& container) {
|
||||
// std::cout << "Sequence container with " << container.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_random_access_container T>
|
||||
// void example_is_random_access_container(T& container) {
|
||||
// if (!container.empty()) {
|
||||
// std::cout << "First element: " << container[0] << std::endl;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_container_adapter T>
|
||||
// void example_is_container_adapter(T& adapter) {
|
||||
// std::cout << "Container adapter with " << adapter.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_associative_container T>
|
||||
// void example_is_associative_container(const T& container) {
|
||||
// std::cout << "Associative container with " << container.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_unordered_associative_container T>
|
||||
// void example_is_unordered_associative_container(const T& container) {
|
||||
// std::cout << "Unordered associative container with " << container.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_tuple_like T>
|
||||
// void example_is_tuple_like(const T& tuple) {
|
||||
// std::cout << "Tuple-like with " << std::tuple_size_v<T> << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_fixed_array T>
|
||||
// void example_is_fixed_array(const T& array) {
|
||||
// std::cout << "Fixed array with " << std::extent_v<T> << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_std_array<T, std::tuple_size_v<T>> T>
|
||||
// void example_is_std_array(const T& array) {
|
||||
// std::cout << "std::array with " << array.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_any_container T>
|
||||
// void example_is_any_container(const T& container) {
|
||||
// std::cout << "Any container with " << container.size() << " elements" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_iterable T>
|
||||
// void example_is_iterable(const T& iterable) {
|
||||
// std::cout << "Iterable object: ";
|
||||
// for (const auto& item : iterable) {
|
||||
// std::cout << item << " ";
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_sized T>
|
||||
// void example_is_sized(const T& sized) {
|
||||
// if constexpr (hack::concepts::modern::has_size<T>) {
|
||||
// std::cout << "Sized object: " << sized.size() << " elements" << std::endl;
|
||||
// } else {
|
||||
// std::cout << "Sized object (compile-time size)" << std::endl;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::has_key_value_semantics T>
|
||||
// void example_has_key_value_semantics(T& container) {
|
||||
// if constexpr (hack::concepts::modern::has_mapped_type<T>) {
|
||||
// std::cout << "Key-value container, sample access demonstrated" << std::endl;
|
||||
// } else {
|
||||
// std::cout << "Set-like container" << std::endl;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::is_contiguous_container T>
|
||||
// void example_is_contiguous_container(const T& container) {
|
||||
// std::cout << "Contiguous memory container" << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::can_emplace<std::vector<int>, int> Container>
|
||||
// void example_can_emplace(Container& container, int value) {
|
||||
// container.emplace_back(value);
|
||||
// std::cout << "Emplaced value: " << value << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::can_push_back<std::vector<int>, int> Container>
|
||||
// void example_can_push_back(Container& container, int value) {
|
||||
// container.push_back(value);
|
||||
// std::cout << "Pushed back value: " << value << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::can_push_front<std::list<int>, int> Container>
|
||||
// void example_can_push_front(Container& container, int value) {
|
||||
// container.push_front(value);
|
||||
// std::cout << "Pushed front value: " << value << std::endl;
|
||||
// }
|
||||
//
|
||||
// template<hack::concepts::modern::can_find<std::set<int>, int> Container>
|
||||
// void example_can_find(Container& container, int value) {
|
||||
// auto it = container.find(value);
|
||||
// if (it != container.end()) {
|
||||
// std::cout << "Found value: " << value << std::endl;
|
||||
// } else {
|
||||
// std::cout << "Value not found: " << value << std::endl;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Функция для демонстрации not_supported
|
||||
// template<typename T>
|
||||
// void check_support(const T& value) {
|
||||
// if constexpr (modern::concepts::not_supported<T>) {
|
||||
// std::cout << "Type is NOT supported by this library" << std::endl;
|
||||
// } else {
|
||||
// std::cout << "Type is supported" << std::endl;
|
||||
// }
|
||||
// }
|
||||
template<hack::concepts::modern::is_string T>
|
||||
void example_is_string(const T& str)
|
||||
{
|
||||
hack::log()("String content: ", str, " (length: ", str.length(), ")");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_sequence_container T>
|
||||
void example_is_sequence_container(const T& container)
|
||||
{
|
||||
hack::log()("Sequence container with ", container.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_random_access_container T>
|
||||
void example_is_random_access_container(T& container)
|
||||
{
|
||||
if (!container.empty())
|
||||
hack::log()("First element: ", container[0]);
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_container_adapter T>
|
||||
void example_is_container_adapter(T& adapter)
|
||||
{
|
||||
hack::log()("Container adapter with ", adapter.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_associative_container T>
|
||||
void example_is_associative_container(const T& container)
|
||||
{
|
||||
hack::log()("Associative container with ", container.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_unordered_associative_container T>
|
||||
void example_is_unordered_associative_container(const T& container)
|
||||
{
|
||||
hack::log()("Unordered associative container with ", container.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_tuple_like T>
|
||||
void example_is_tuple_like(const T& tuple)
|
||||
{
|
||||
hack::log()("Tuple-like with ", std::tuple_size_v<T>, " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_fixed_array T>
|
||||
void example_is_fixed_array(const T& array)
|
||||
{
|
||||
hack::log()("Fixed array with ", std::extent_v<T>, " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_std_array T>
|
||||
void example_is_std_array(const T& array)
|
||||
{
|
||||
hack::log()("std::array with ", array.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_any_container T>
|
||||
void example_is_any_container(const T& container)
|
||||
{
|
||||
hack::log()("Any container with ", container.size(), " elements");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_iterable T>
|
||||
void example_is_iterable(const T& iterable)
|
||||
{
|
||||
hack::log()("Iterable object");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_sized T>
|
||||
void example_is_sized(const T& sized)
|
||||
{
|
||||
if constexpr (hack::concepts::modern::has_size<T>)
|
||||
hack::log()("Sized object: ", sized.size(), " elements");
|
||||
else
|
||||
hack::log()("Sized object (compile-time size)");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void check_support(const T& value)
|
||||
{
|
||||
if constexpr (hack::concepts::modern::not_supported<T>)
|
||||
hack::log()("Type is NOT supported by this library");
|
||||
else
|
||||
hack::log()("Type is supported");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::has_key_value_semantics T>
|
||||
void example_has_key_value_semantics(T& container)
|
||||
{
|
||||
if constexpr (hack::concepts::modern::has_mapped_type<T>)
|
||||
hack::log()("Key-value container, sample access demonstrated");
|
||||
else
|
||||
hack::log()("Set-like container");
|
||||
}
|
||||
|
||||
template<hack::concepts::modern::is_contiguous_container T>
|
||||
void example_is_contiguous_container(const T& container)
|
||||
{
|
||||
hack::log()("Contiguous memory container");
|
||||
}
|
||||
|
||||
template<typename Container, typename Value>
|
||||
requires hack::concepts::modern::can_push_front<Container, Value>
|
||||
void example_can_push_front(Container& container, Value&& value)
|
||||
{
|
||||
container.push_front(std::forward<Value>(value));
|
||||
hack::log()("Pushed front value: ", value);
|
||||
}
|
||||
|
||||
template<typename Container, typename Value>
|
||||
requires hack::concepts::modern::can_push_back<Container, Value>
|
||||
void example_can_push_back(Container& container, Value&& value)
|
||||
{
|
||||
container.push_back(std::forward<Value>(value));
|
||||
hack::log()("Pushed back value: ", value);
|
||||
}
|
||||
|
||||
template<typename Container, typename Key>
|
||||
requires hack::concepts::modern::can_find<Container, Key>
|
||||
void example_can_find(Container& container, Key&& key)
|
||||
{
|
||||
auto it = container.find(std::forward<Key>(key));
|
||||
if (it != container.end())
|
||||
hack::log()("Found value: ", *it);
|
||||
else
|
||||
hack::log()("Value not found: ", key);
|
||||
}
|
||||
|
||||
auto main(int argc, char *argv[]) -> int
|
||||
{
|
||||
// Базовые концепты
|
||||
std::vector<int> vec = { 1, 2, 3 };
|
||||
std::map<int, std::string> map = {{ 1, "one" }, { 2, "two" }};
|
||||
std::set<int> set = { 5, 3, 1, 4, 2 };
|
||||
std::unordered_map<int, std::string> umap = {{ 1, "uno" }, { 2, "dos" }};
|
||||
std::list<double> list = { 1.1, 2.2, 3.3 };
|
||||
std::deque<char> deque = { 'a', 'b', 'c' };
|
||||
std::stack<int> stack; stack.push(1); stack.push(2);
|
||||
std::string str = "Hello";
|
||||
std::tuple<int, std::string, double> tuple = {1, "test", 3.14};
|
||||
int fixed_array[5] = {1, 2, 3, 4, 5};
|
||||
std::array<float, 3> std_array = {1.0f, 2.0f, 3.0f};
|
||||
int a = 10;
|
||||
struct custom_type { int x; };
|
||||
custom_type custom;
|
||||
|
||||
hack::log()("has_value_type:");
|
||||
example_has_value_type(vec);
|
||||
// example_has_value_type(a); // выдаст ошибку т.к. у a нет встроенного value_type
|
||||
|
||||
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<double> list = {1.1, 2.2, 3.3};
|
||||
// std::deque<char> deque = {'a', 'b', 'c'};
|
||||
//
|
||||
// std::cout << "6. is_string: ";
|
||||
// example_is_string(str);
|
||||
//
|
||||
// std::cout << "7. is_sequence_container (vector): ";
|
||||
// example_is_sequence_container(vec);
|
||||
//
|
||||
// std::cout << "8. is_sequence_container (list): ";
|
||||
// example_is_sequence_container(list);
|
||||
//
|
||||
// std::cout << "9. is_random_access_container: ";
|
||||
// example_is_random_access_container(vec);
|
||||
//
|
||||
// // Адаптеры и ассоциативные контейнеры
|
||||
// std::stack<int> stack;
|
||||
// stack.push(1); stack.push(2);
|
||||
//
|
||||
// std::cout << "10. is_container_adapter: ";
|
||||
// example_is_container_adapter(stack);
|
||||
//
|
||||
// std::cout << "11. is_associative_container: ";
|
||||
// example_is_associative_container(set);
|
||||
//
|
||||
// std::cout << "12. is_unordered_associative_container: ";
|
||||
// example_is_unordered_associative_container(umap);
|
||||
//
|
||||
// // Кортежи и массивы
|
||||
// std::tuple<int, std::string, double> tuple = {1, "test", 3.14};
|
||||
// int fixed_array[5] = {1, 2, 3, 4, 5};
|
||||
// std::array<float, 3> std_array = {1.0f, 2.0f, 3.0f};
|
||||
//
|
||||
// std::cout << "13. is_tuple_like: ";
|
||||
// example_is_tuple_like(tuple);
|
||||
//
|
||||
// std::cout << "14. is_fixed_array: ";
|
||||
// example_is_fixed_array(fixed_array);
|
||||
//
|
||||
// std::cout << "15. is_std_array: ";
|
||||
// example_is_std_array(std_array);
|
||||
//
|
||||
// // Универсальные концепты
|
||||
// std::cout << "16. is_any_container: ";
|
||||
// example_is_any_container(vec);
|
||||
//
|
||||
// std::cout << "17. is_iterable: ";
|
||||
// example_is_iterable(vec);
|
||||
//
|
||||
// std::cout << "18. is_sized: ";
|
||||
// example_is_sized(vec);
|
||||
//
|
||||
// std::cout << "19. has_key_value_semantics: ";
|
||||
// example_has_key_value_semantics(map);
|
||||
//
|
||||
// std::cout << "20. is_contiguous_container: ";
|
||||
// example_is_contiguous_container(vec);
|
||||
//
|
||||
// // Концепты для алгоритмов
|
||||
// std::cout << "21. can_emplace: ";
|
||||
// example_can_emplace(vec, 42);
|
||||
//
|
||||
// std::cout << "22. can_push_back: ";
|
||||
// example_can_push_back(vec, 99);
|
||||
//
|
||||
// std::cout << "23. can_push_front: ";
|
||||
// example_can_push_front(list, 0);
|
||||
//
|
||||
// std::cout << "24. can_find: ";
|
||||
// example_can_find(set, 3);
|
||||
//
|
||||
// // Проверка неподдерживаемых типов
|
||||
// struct CustomType { int x; };
|
||||
// CustomType custom;
|
||||
//
|
||||
// std::cout << "25. not_supported check (int): ";
|
||||
// check_support(42);
|
||||
//
|
||||
// std::cout << "26. not_supported check (CustomType): ";
|
||||
// check_support(custom);
|
||||
//
|
||||
// std::cout << "\n=== Все примеры выполнены ===" << std::endl;
|
||||
example_is_string(str);
|
||||
example_is_sequence_container(vec);
|
||||
example_is_sequence_container(list);
|
||||
example_is_random_access_container(vec);
|
||||
example_is_container_adapter(stack);
|
||||
example_is_associative_container(set);
|
||||
example_is_unordered_associative_container(umap);
|
||||
example_is_tuple_like(tuple);
|
||||
example_is_fixed_array(fixed_array);
|
||||
example_is_std_array(std_array);
|
||||
example_is_any_container(vec);
|
||||
example_is_iterable(vec);
|
||||
example_is_sized(vec);
|
||||
check_support(a);
|
||||
check_support(custom);
|
||||
example_has_key_value_semantics(map);
|
||||
example_is_contiguous_container(vec);
|
||||
example_can_push_front(list, 0);
|
||||
example_can_push_back(vec, 99);
|
||||
example_can_find(set, 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user