From 359e82b396c6cf0c4cd66de48cbc844c55d1896f Mon Sep 17 00:00:00 2001 From: youspectrum <2725096176@qq.com> Date: Thu, 29 Sep 2022 16:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E4=BB=BF=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=81=E5=87=BD=E6=95=B0=E6=A8=A1=E6=9D=BF=E3=80=81?= =?UTF-8?q?=E6=88=90=E5=91=98=E6=A8=A1=E6=9D=BF=E3=80=81=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=89=B9=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- cmake-build-debug/.ninja_deps | Bin 11472 -> 9972 bytes cmake-build-debug/.ninja_log | 4 -- .../Testing/Temporary/LastTest.log | 4 +- h/function_like_classes.h | 16 +++++ h/function_template.h | 15 +++++ h/member_template.h | 22 +++++++ h/shared_ptr.h | 4 +- h/specialization.h | 61 ++++++++++++++++++ main.cpp | 9 +++ 10 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 h/function_like_classes.h create mode 100644 h/function_template.h create mode 100644 h/member_template.h create mode 100644 h/specialization.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d63891..424e376 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(MSTL) set(CMAKE_CXX_STANDARD 14) -add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h h/shared_ptr.h) +add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h h/shared_ptr.h h/function_like_classes.h h/function_template.h h/member_template.h h/specialization.h) diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index 76ac69f5854de4dbf3bd748d6a906dd0a9d6edbf..3549587d5f486e3604a257c954bd230579f4f696 100644 GIT binary patch delta 22 ecmcZ*`Nem`4P{1+$%@L-lOL!_Y<{8nf(Za~^a+pv delta 53 zcmez3dm(be4Q0L;ObiVSKyc1KNaqaeW+9b(OpF?n6_uqYe^HiT +struct identity{ + const T& operator()(const T& x) const { + return x; + } +}; \ No newline at end of file diff --git a/h/function_template.h b/h/function_template.h new file mode 100644 index 0000000..a8a4ec3 --- /dev/null +++ b/h/function_template.h @@ -0,0 +1,15 @@ +// +// Created by dongl on 22-9-29. +// + +#ifndef MSTL_TEM_FUNCTION_H +#define MSTL_TEM_FUNCTION_H + +#endif //MSTL_TEM_FUNCTION_H + + +/// 函数模板 +template +T& min(const T& var1, const T& var2) { + return var1 < var2 ? var2 : var1; +} diff --git a/h/member_template.h b/h/member_template.h new file mode 100644 index 0000000..152476a --- /dev/null +++ b/h/member_template.h @@ -0,0 +1,22 @@ +// +// Created by dongl on 22-9-29. +// + +#ifndef MSTL_MEMBER_TEMPLATE_H +#define MSTL_MEMBER_TEMPLATE_H + +#endif //MSTL_MEMBER_TEMPLATE_H + +/// 成员模板 +template +struct pair{ + T1 first; + T2 second; + + pair() : first(T1()), second(T2()){} + pair(const T1& a, const T2& b) : first(a), second(b) {} + + // 成员模板 + template + pair(const pair& p) : first(p.first), second(p.second){} +}; \ No newline at end of file diff --git a/h/shared_ptr.h b/h/shared_ptr.h index d426fc0..d5a8d0e 100644 --- a/h/shared_ptr.h +++ b/h/shared_ptr.h @@ -23,4 +23,6 @@ public: private: T* px; long* pn; -}; \ No newline at end of file +}; + + diff --git a/h/specialization.h b/h/specialization.h new file mode 100644 index 0000000..999a9b2 --- /dev/null +++ b/h/specialization.h @@ -0,0 +1,61 @@ +// +// Created by dongl on 22-9-29. +// + +#include + +#ifndef MSTL_SPECIALIZATION_H +#define MSTL_SPECIALIZATION_H + +#endif //MSTL_SPECIALIZATION_H + +/// 模板特化 specialization +template +struct hash{ + +}; + +template<> +struct hash{ + size_t operator() (char x) const { + return x; + } +}; + +template<> +struct hash{ + size_t operator() (int x) const { + return x; + } +}; + +template<> +struct hash{ + size_t operator() (long x) const { + return x; + } +}; + + +/// partial specialization 模板偏特化 局部特化 个数上的偏 +template +class m_vector{ + +}; + +template +class m_vector{ + +}; + + +/// partial specialization 模板偏特化 局部特化 范围上的偏 +template +class C{ + +}; + +template +class C{ + +}; diff --git a/main.cpp b/main.cpp index 814692b..48fa9d1 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include "h/String.h" #include "h/Faction.h" #include "h/shared_ptr.h" +#include "h/function_like_classes.h" +#include "h/specialization.h" int main() { List list = *new List; @@ -18,5 +20,12 @@ int main() { shared_ptr ptr(new String("ptr")); std::cout << ptr->get_c_str() << std::endl; + + String str("fun_str"); + +// identity ity()(1000); + + hash()(1000); + return 0; } \ No newline at end of file