add some example conceptes
This commit is contained in:
@@ -4,29 +4,56 @@
|
|||||||
template<hack::concepts::modern::has_value_type T>
|
template<hack::concepts::modern::has_value_type T>
|
||||||
void example_has_value_type(const T& container)
|
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());
|
hack::log()("Value type: ", typeid(typename T::value_type).name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// template<hack::concepts::modern::has_key_type T>
|
template<hack::concepts::modern::has_key_type T>
|
||||||
// void example_has_key_type(const T& container) {
|
void example_has_key_type(const T& container)
|
||||||
// std::cout << "Key type: " << typeid(typename T::key_type).name() << std::endl;
|
{
|
||||||
// }
|
hack::log()("Key type: ", typeid(typename T::key_type).name());
|
||||||
//
|
}
|
||||||
// template<hack::concepts::modern::has_mapped_type T>
|
|
||||||
// void example_has_mapped_type(const T& container) {
|
template<hack::concepts::modern::has_mapped_type T>
|
||||||
// std::cout << "Mapped type: " << typeid(typename T::mapped_type).name() << std::endl;
|
void example_has_mapped_type(const T& container)
|
||||||
// }
|
{
|
||||||
//
|
hack::log()("Mapped type: ", typeid(typename T::mapped_type).name());
|
||||||
// template<hack::concepts::modern::has_iterator T>
|
}
|
||||||
// void example_has_iterator(const T& container) {
|
|
||||||
// std::cout << "Container is iterable, size: " << std::distance(container.begin(), container.end()) << std::endl;
|
template<hack::concepts::modern::has_iterator T>
|
||||||
// }
|
void example_has_iterator(const T& container)
|
||||||
//
|
{
|
||||||
// template<hack::concepts::modern::has_size T>
|
hack::log()("Container is iterable, size: ", std::distance(container.begin(), container.end()));
|
||||||
// void example_has_size(const T& container) {
|
}
|
||||||
// std::cout << "Container size: " << container.size() << std::endl;
|
|
||||||
// }
|
template<hack::concepts::modern::has_size T>
|
||||||
//
|
void example_has_size(const T& container)
|
||||||
|
{
|
||||||
|
hack::log()("Container size:", container.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<hack::concepts::modern::has_key_compare T>
|
||||||
|
void example_has_key_compare(const T& container)
|
||||||
|
{
|
||||||
|
hack::log()("Key compare type: ", typeid(typename T::key_compare).name());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<hack::concepts::modern::has_allocator_type T>
|
||||||
|
void example_has_allocator_type(const T& container)
|
||||||
|
{
|
||||||
|
// Можно получить аллокатор из контейнера
|
||||||
|
typename T::allocator_type alloc = container.get_allocator();
|
||||||
|
hack::log()("Allocator obtained successfully");
|
||||||
|
}
|
||||||
|
|
||||||
// template<hack::concepts::modern::is_string T>
|
// template<hack::concepts::modern::is_string T>
|
||||||
// void example_is_string(const T& str) {
|
// void example_is_string(const T& str) {
|
||||||
// std::cout << "String content: " << str << " (length: " << str.length() << ")" << std::endl;
|
// std::cout << "String content: " << str << " (length: " << str.length() << ")" << std::endl;
|
||||||
@@ -154,24 +181,33 @@ auto main(int argc, char *argv[]) -> int
|
|||||||
// Базовые концепты
|
// Базовые концепты
|
||||||
std::vector<int> vec = { 1, 2, 3 };
|
std::vector<int> vec = { 1, 2, 3 };
|
||||||
std::map<int, std::string> map = {{ 1, "one" }, { 2, "two" }};
|
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" }};
|
||||||
int a = 10;
|
int a = 10;
|
||||||
|
|
||||||
hack::log()("has_value_type:");
|
hack::log()("has_value_type:");
|
||||||
example_has_value_type(vec);
|
example_has_value_type(vec);
|
||||||
// example_has_value_type(a); // выдаст ошибку т.к. у a нет встроенного value_type
|
// example_has_value_type(a); // выдаст ошибку т.к. у a нет встроенного value_type
|
||||||
|
|
||||||
// std::cout << "2. has_key_type: ";
|
hack::log()("has_key_type:");
|
||||||
// example_has_key_type(map);
|
example_has_key_type(map);
|
||||||
//
|
|
||||||
// std::cout << "3. has_mapped_type: ";
|
hack::log()("has_mapped_type:");
|
||||||
// example_has_mapped_type(map);
|
example_has_mapped_type(map);
|
||||||
//
|
// example_has_mapped_type(set); // выдаст ошибку т.к. у set нет типа mapped_type. Этот тип характерен для контейнеров, которые хранят пары "ключ-значение".
|
||||||
// std::cout << "4. has_iterator: ";
|
|
||||||
// example_has_iterator(vec);
|
hack::log()("has_iterator:");
|
||||||
//
|
example_has_iterator(vec);
|
||||||
// std::cout << "5. has_size: ";
|
|
||||||
// example_has_size(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::string str = "Hello";
|
||||||
// std::list<double> list = {1.1, 2.2, 3.3};
|
// std::list<double> list = {1.1, 2.2, 3.3};
|
||||||
@@ -193,9 +229,6 @@ auto main(int argc, char *argv[]) -> int
|
|||||||
// std::stack<int> stack;
|
// std::stack<int> stack;
|
||||||
// stack.push(1); stack.push(2);
|
// stack.push(1); stack.push(2);
|
||||||
//
|
//
|
||||||
// std::set<int> set = {5, 3, 1, 4, 2};
|
|
||||||
// std::unordered_map<int, std::string> umap = {{1, "uno"}, {2, "dos"}};
|
|
||||||
//
|
|
||||||
// std::cout << "10. is_container_adapter: ";
|
// std::cout << "10. is_container_adapter: ";
|
||||||
// example_is_container_adapter(stack);
|
// example_is_container_adapter(stack);
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user